Nginx Redis srcache PHP-FPM架构请求流程客户端发起 HTTP 请求 → Nginx 接收。Nginx 先检查 Redis 中是否有对应缓存命中直接从 Redis 读取内容并返回给客户端无需 PHP 参与。未命中将请求转发给 PHP-FPM 处理PHP 生成内容后Nginx 再将其缓存到 Redis同时返回给客户端。核心模块ngx_http_redis_module让 Nginx 直接与 Redis 通信读取缓存。ngx_http_srcache_module负责将 PHP 生成的响应内容缓存到 Redis。流程图源码安装nginx[rootnginx ~]# wget https://nginx.org/download/nginx-1.28.1.tar.gz[rootnginx ~]# tar zxf nginx-1.28.1.tar.gz[rootnginx ~]# cd nginx-1.28.1/#安装依赖[rootnginx ~]# dnf install gcc pcre-devel zlib-devel openssl-devel -y#选择要使用的模块[rootnginx nginx-1.28.1]# ./configure --prefix/usr/local/nginx --usernginx --groupnginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module --add-module/root/echo-nginx-module-0.64 --add-module/root/redis2-nginx-module-0.15 --add-module/root/srcache-nginx-module-0.33[rootnginx nginx-1.28.1]# make make install#创建运行用户[rootnginx]# useradd -s /sbin/nologin -M nginx#设定环境变量[rootnginx sbin]# vim ~/.bash_profileexportPATH$PATH:/usr/local/nginx/sbin[rootnginx sbin]# source ~/.bash_profile#编写Nginx启动文件systemd[rootnginx ~]# cd /lib/systemd/system[rootnginx system]# vim nginx.service[Unit]DescriptionThe NGINX HTTP and reverse proxy serverAftersyslog.target network.target remote-fs.target nss-lookup.target[Service]TypeforkingPIDFile/usr/local/nginx/logs/nginx.pid#指定nginx启动的pidExecStartPre/usr/local/nginx/sbin/nginx -t#指定nginx -t检查配置文件命令ExecStart/usr/local/nginx/sbin/nginx#指定nginx启动命令ExecReload/bin/kill -s HUP$MAINPIDExecStop/bin/kill -s QUIT$MAINPIDPrivateTmptrue[Install]WantedBymulti-user.target#使编写的配置生效[rootnginx system]# systemctl daemon-reload#在启动时要确保nginx已经关闭不然会冲突导致报错[rootnginx system]# systemctl enable --now nginx源码安装php[rootnginx ~]# wget https://www.php.net/distributions/php-8.3.30.tar.gz[rootnginx ~]# wget https://mirrors.aliyun.com/rockylinux/9.7/devel/x86_64/os/Packages/o/oniguruma-devel-6.9.6-1.el9.6.x86_64.rpm #下载依赖包[rootnginx ~]# tar zxf php-8.3.30.tar.gz#安装依赖[rootnginx ~]# dnf install gcc systemd-devel-252-51.el9.x86_64 libxml2-devel.x86_64 sqlite-devel.x86_64 libcurl-devel.x86_64 libpng-devel.x86_64 oniguruma-devel-6.9.6-1.el9.6.x86_64.rpm -y[rootnginx php-8.3.30]# ./configure \--prefix/usr/local/php\#安装路径--with-config-file-path/usr/local/php/etc\#指定配置路径--enable-fpm\#用cgi方式启动程序--with-fpm-usernginx\#指定运行用户身份--with-fpm-groupnginx\--with-curl\#打开curl浏览器支持--with-iconv\#启用iconv函数转换字符编码--with-mhash\#mhash加密方式扩展库--with-zlib\#支持zlib库用于压缩http压缩传输--with-openssl\#支持ssl加密--enable-mysqlnd\#mysql数据库--with-mysqli\--with-pdo-mysql\--disable-debug\#关闭debug功能--enable-sockets\#支持套接字访问--enable-soap\#支持soap扩展协议--enable-xml\#支持xml--enable-ftp\#支持ftp--enable-gd\#支持gd库--enable-exif\#支持图片元数据--enable-mbstring\#支持多字节字符串--enable-bcmath\#打开图片大小调整,用到zabbix监控的时候用到了这个模块--with-fpm-systemd#支持systemctl 管理cgi[rootnginx php-8.3.30]# make make instsall配置优化php[rootnginx ~]# cd /usr/local/php/etc[rootnginx etc]# cp php-fpm.conf.default php-fpm.conf #复制模板[rootnginx etc]# vim php-fpm.conf#去掉注释pidrun/php-fpm.pid#指定pid文件存放位置[rootnginx etc]# cd php-fpm.d/[rootnginx php-fpm.d]# cp www.conf.default www.conf[rootnginx php-fpm.d]# vim www.conf41listen0.0.0.0:9000#可以修改端口#生成主配置文件[rootnginx php-8.3.30]# cp php.ini-production /usr/local/php/etc/php.ini[rootnginx ~]# vim /usr/local/php/etc/php.ini989date.timezoneAsia/Shanghai#修改时区#生成启动文件[rootnginx etc]# cp ~/php-8.3.30/sapi/fpm/php-fpm.service /lib/systemd/system/[rootnginx etc]# vim /lib/systemd/system/php-fpm.service#ProtectSystemfull #注释该内容[rootnginx etc]# systemctl daemon-reload[rootnginx etc]# systemctl enable --now php-fpm.service[rootnginx etc]# netstat -antlupe | grep phptcp00127.0.0.1:90000.0.0.0:* LISTEN0120615147595/php-fpm: masrpm包安装redis[rootnginx ~]# dnf install redis -ynginx配置整合redis与php[rootnginx ~]# vim /usr/local/nginx/conf.d/php.confupstream redis{server127.0.0.1:6379;keepalive10;}server{listen80;server_name php.fjw.org;root /webdir/fjw.org/php/html;location /redis{internal;set$redis_key$query_string;redis2_query get$redis_key;redis2_query setex$redis_key300$srcache_response_body;redis2_pass redis;}location ~\.php${set$key$uri$args;srcache_fetch GET /redis$key;srcache_store PUT /redis$key;fastcgi_pass127.0.0.1:9000;fastcgi_index index.php;include fastcgi.conf;}}[rootnginx ~]# nginx -s reload测试编写测试文件[rootnginx ~]# vim /webdir/fjw.org/php/html/test.php?phpechoGenerated by PHP at .date(Y-m-d H:i:s);?访问测试第一次访问http://服务器IP/test.php页面显示当前时间此时 Nginx 缓存未命中请求由 PHP 处理并缓存到 Redis。刷新页面若显示的时间与第一次相同说明缓存命中Nginx 直接从 Redis 返回内容绕过了 PHP。