Nginx中fastcgi_cache实现PHP页面缓存加速
更新时间:2026年04月20日 09:27:42 作者:冰川箭仙
Nginx使用cgi_cache缓存PHP生成的动态页面,降低后端负载并提升响应速度,配置方案包括缓存控制、自定义缓存时间、缓存清理等等关键响应头,感兴趣的可以了解一下
在 Nginx 中使用 fastcgi_cache 可以缓存 PHP-FPM 生成的动态页面,显著降低后端负载并提升响应速度。以下是完整的配置方案和优化技巧。
核心配置架构
用户请求 → Nginx → 检查缓存 → [命中] 直接返回缓存
↓
[未命中] → PHP-FPM → 生成页面 → 存入缓存 → 返回用户
基础配置
1. http 块(全局配置)
http {
# 定义缓存区
fastcgi_cache_path /var/cache/nginx/fastcgi levels=1:2 keys_zone=PHPCACHE:100m max_size=1g inactive=60m use_temp_path=off;
# 缓存键定义(区分 URI 和参数)
fastcgi_cache_key "$scheme$request_method$host$request_uri";
# 默认缓存时间
fastcgi_cache_valid 200 302 10m;
fastcgi_cache_valid 404 1m;
# 日志格式(可选,用于调试缓存命中率)
log_format cache_log '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'cache:$upstream_cache_status';
}2. server/location 块(站点配置)
server {
listen 80;
server_name example.com;
root /var/www/html;
index index.php;
# PHP 请求处理
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
# ========== 缓存核心配置 ==========
# 启用缓存并指定缓存区
fastcgi_cache PHPCACHE;
# 缓存条件:仅对登录用户以外的请求缓存
fastcgi_cache_bypass $cookie_session; # 有 session cookie 时不读缓存
fastcgi_no_cache $cookie_session; # 有 session cookie 时不写缓存
# 或者根据自定义 header 控制(如管理员后台不缓存)
fastcgi_cache_bypass $http_pragma;
fastcgi_no_cache $http_authorization;
# 缓存状态头(调试用,生产环境可移除)
add_header X-Cache-Status $upstream_cache_status;
# 缓存锁定(防止缓存失效时的并发穿透)
fastcgi_cache_lock on;
fastcgi_cache_lock_timeout 5s;
# 后台更新(旧缓存过期时,先返回旧数据,后台异步更新)
fastcgi_cache_background_update on;
# 使用 stale 缓存(后端故障时返回过期缓存)
fastcgi_cache_use_stale error timeout invalid_header updating http_500;
}
# 静态文件不经过 PHP
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
access_log off;
}
}缓存控制策略详解
按 Cookie 排除(用户登录状态)
# 如果有 wordpress_logged_in cookie 就不缓存(WordPress 场景)
fastcgi_cache_bypass $cookie_wordpress_logged_in;
fastcgi_no_cache $cookie_wordpress_logged_in;
# 多条件组合(任一满足即不缓存)
set $skip_cache 0;
if ($cookie_session) { set $skip_cache 1; }
if ($request_method = POST) { set $skip_cache 1; }
if ($query_string != "") { set $skip_cache 1; }
if ($http_authorization) { set $skip_cache 1; }
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;按 URL 排除(后台管理、API)
location ~ /wp-admin|/wp-login|/api/ {
fastcgi_cache off; # 完全禁用缓存
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
include fastcgi_params;
}自定义缓存时间(按页面类型)
location ~ \.php$ {
fastcgi_cache PHPCACHE;
# 首页缓存 5 分钟
if ($request_uri = /) {
set $cache_time 5m;
}
# 文章页缓存 1 小时
if ($request_uri ~ ^/post/) {
set $cache_time 1h;
}
# 默认 10 分钟
if ($cache_time = "") {
set $cache_time 10m;
}
fastcgi_cache_valid 200 $cache_time;
}缓存清理方案
方案 1:使用 ngx_cache_purge 模块(需编译安装)
# 编译时添加 --add-module=ngx_cache_purge
location ~ /purge(/.*) {
allow 127.0.0.1; # 仅允许本地访问
deny all;
fastcgi_cache_purge PHPCACHE "$scheme$request_method$host$1";
}方案 2:脚本手动清理
#!/bin/bash
# /usr/local/bin/clear-php-cache.sh
CACHE_DIR="/var/cache/nginx/fastcgi"
TARGET="$1"
if [ -z "$TARGET" ]; then
# 清理全部缓存
rm -rf ${CACHE_DIR}/*
echo "All cache cleared"
else
# 根据 URL 计算缓存文件路径(匹配 fastcgi_cache_key)
KEY="$scheme$request_method$host$TARGET"
MD5=$(echo -n "$KEY" | md5sum | awk '{print $1}')
LEVEL1=${MD5: -1}
LEVEL2=${MD5: -3:2}
rm -f "${CACHE_DIR}/${LEVEL1}/${LEVEL2}/${MD5}"
echo "Cache for $TARGET cleared"
fi
# 重载 Nginx 使更改生效(或发送信号)
kill -HUP $(cat /var/run/nginx.pid)生产环境优化配置
http {
# 缓存路径优化:SSD 建议用 levels=1:2,机械硬盘用 levels=1:2:3
fastcgi_cache_path /var/cache/nginx/fastcgi
levels=1:2
keys_zone=PHPCACHE:256m # 内存中存储 256MB 的键
max_size=10g # 磁盘最大 10GB
inactive=7d # 7 天未访问则清理
use_temp_path=off # 直接写入目标目录,减少 IO
manager_files=10000 # 每次清理最多处理 10000 文件
manager_sleep=50ms # 清理间隔
manager_threshold=200ms; # 单次清理最大耗时
# 忽略后端设置的缓存控制头(强制按 Nginx 配置缓存)
fastcgi_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;
# 隐藏后端版本信息
fastcgi_hide_header X-Powered-By;
fastcgi_hide_header Server;
}
server {
location ~ \.php$ {
fastcgi_cache PHPCACHE;
fastcgi_cache_valid 200 5m;
# 微缓存(Microcache):极短时间的缓存,应对突发流量
fastcgi_cache_valid 200 1s; # 1 秒内相同请求直接返回缓存
# 缓存键增加设备类型(桌面/移动端分开缓存)
fastcgi_cache_key "$scheme$request_method$host$request_uri$http_user_agent";
}
}监控与调试
查看缓存命中率
# 查看缓存目录统计
find /var/cache/nginx/fastcgi -type f | wc -l
# 实时查看缓存状态头
curl -I http://example.com/page.php
# 检查响应头中的 X-Cache-Status: HIT / MISS / BYPASS / EXPIRED
# 分析日志统计命中率
awk '{print $NF}' /var/log/nginx/access.log | sort | uniq -c关键响应头说明
| 状态 | 含义 |
|---|---|
| HIT | 缓存命中,直接返回 |
| MISS | 缓存未命中,已写入新缓存 |
| BYPASS | 跳过缓存(如带有 Cookie) |
| EXPIRED | 缓存过期,已重新获取 |
| UPDATING | 缓存过期,但返回旧数据(后台更新中) |
| STALE | 返回过期缓存(后端故障时) |
完整 WordPress 优化示例
fastcgi_cache_path /var/cache/nginx/wp levels=1:2 keys_zone=WPCACHE:150m max_size=2g inactive=48h;
server {
set $skip_cache 0;
# 不缓存的条件
if ($request_method = POST) { set $skip_cache 1; }
if ($query_string != "") { set $skip_cache 1; }
if ($cookie_woocommerce_items_in_cart) { set $skip_cache 1; } # WooCommerce
if ($cookie_wordpress_logged_in) { set $skip_cache 1; }
if ($request_uri ~* "/wp-admin/|/wp-login|/cart|/checkout|/my-account") { set $skip_cache 1; }
location ~ \.php$ {
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_cache WPCACHE;
fastcgi_cache_valid 200 60m;
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
fastcgi_cache_lock on;
fastcgi_cache_use_stale updating error timeout;
add_header X-Cache-Status $upstream_cache_status;
}
}配置完成后,使用 nginx -t 检查语法,然后 systemctl reload nginx 生效。建议先用小流量测试,确认缓存逻辑符合预期后再全量上线。
到此这篇关于Nginx中fastcgi_cache实现PHP页面缓存加速的文章就介绍到这了,更多相关Nginx PHP页面缓存加速内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
您可能感兴趣的文章:


最新评论