PHP-FPM 的 FastCGI 缓存清除
为 WordPress 提供超快速缓存,并自动进行精确的缓存失效。
-
无需代理
FastCGI 缓存直接与 PHP-FPM 通信——没有反向代理的开销
-
微秒响应
缓存页面直接从内存或 SSD 由 NGINX 提供
-
选择性清除
仅清除已更改的页面,其余缓存保持热状态
-
内置安全性
为登录用户、管理员和 WooCommerce 购物车绕过缓存
何时使用 FastCGI 与代理缓存
| 特性 | FastCGI 缓存 | 代理缓存 |
|---|---|---|
| 后端 | PHP-FPM(直接) | 任何 HTTP 后端 |
| 延迟 | 较低(无代理跳转) | 略高 |
| 使用场景 | WordPress、PHP 应用 | Node.js、Python 等 |
| 配置复杂性 | 更简单 | 更灵活 |
如果您在没有反向代理层的情况下直接在 NGINX 上运行 WordPress 或 PHP,请使用 FastCGI 缓存。
快速设置
第一步:安装所需模块
# 安装 GetPageSpeed 仓库
dnf -y install https://extras.getpagespeed.com/release-latest.rpm
# 安装缓存清除模块
dnf -y install nginx-module-cache-purge
在 /etc/nginx/nginx.conf 中启用:
load_module modules/ngx_http_cache_purge_module.so;
第二步:配置 FastCGI 缓存
在 /etc/nginx/nginx.conf 的 http 块中添加:
# FastCGI 缓存区域定义
fastcgi_cache_path /var/cache/nginx/fastcgi
levels=1:2
keys_zone=WORDPRESS:100m
max_size=10g
inactive=60m
use_temp_path=off;
# 缓存键(使每个缓存页面唯一的内容)
fastcgi_cache_key "$scheme$request_method$host$request_uri";
第三步:配置服务器块
创建或更新您的 WordPress 服务器块:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com;
index index.php;
# 缓存设置
set $skip_cache 0;
# 不缓存 POST 请求
if ($request_method = POST) {
set $skip_cache 1;
}
# 不缓存带有查询字符串的 URL
if ($query_string != "") {
set $skip_cache 1;
}
# 不缓存 WordPress 管理、登录或特定路径
if ($request_uri ~* "/wp-admin/|/wp-login.php|/xmlrpc.php|/wp-cron.php") {
set $skip_cache 1;
}
# 不缓存登录用户或最近评论者
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in") {
set $skip_cache 1;
}
# 不缓存 WooCommerce 页面
if ($request_uri ~* "/cart/|/checkout/|/my-account/") {
set $skip_cache 1;
}
if ($http_cookie ~* "woocommerce_cart_hash|woocommerce_items_in_cart") {
set $skip_cache 1;
}
# 缓存状态头
add_header X-Cache-Status $upstream_cache_status;
# 启用 PURGE 方法
fastcgi_cache_purge PURGE from 127.0.0.1;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
# FastCGI 缓存
fastcgi_cache WORDPRESS;
fastcgi_cache_valid 200 60m;
fastcgi_cache_valid 404 1m;
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
# 动态内容的重要性
fastcgi_cache_use_stale error timeout invalid_header http_500 http_503;
fastcgi_cache_lock on;
}
# 静态文件 - 不需要缓存配置
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}
}
创建缓存目录并重新加载:
mkdir -p /var/cache/nginx/fastcgi
chown nginx:nginx /var/cache/nginx/fastcgi
nginx -t && systemctl reload nginx
第四步:安装 WordPress 插件
- 转到 插件 → 添加新插件
- 搜索 "Proxy Cache Purge"
- 安装并激活
wp plugin install varnish-http-purge --activate
配置插件:
- 转到 设置 → Proxy Cache Purge
- 将 自定义 IP 设置为
127.0.0.1 - 保存
第五步:配置 NGINX 缓存后端
从 Proxy Cache Purge 5.9.0 开始,插件原生支持 NGINX 的通配符清除格式。配置它以使用 NGINX 后端:
- 转到 Proxy Cache Purge → 设置
- 在 缓存后端 下,选择 NGINX
- 点击 保存设置
添加到您的 wp-config.php:
define( 'VHP_PURGE_BACKEND', 'nginx' );
遗留:插件版本 < 5.9.0
如果您运行的 Proxy Cache Purge 版本低于 5.9.0,请创建
wp-content/mu-plugins/nginx-cache-purge-fix.php:
<?php
/**
* 插件名称:NGINX FastCGI 缓存清除修复
* 描述:附加通配符以清除所有缓存变体
*/
add_filter("vhp_purgeme_path", function($purgeme, $schema, $host, $path, $pregex, $p) {
if (empty($pregex)) {
$purgeme .= "*";
}
return $purgeme;
}, 10, 6);
测试
# 第一次请求 - 应该是 MISS
curl -sI http://127.0.0.1/ -H 'Host: example.com' | grep X-Cache
# X-Cache-Status: MISS
# 第二次请求 - 应该是 HIT
curl -sI http://127.0.0.1/ -H 'Host: example.com' | grep X-Cache
# X-Cache-Status: HIT
# 清除缓存
curl -sX PURGE 'http://127.0.0.1/*' -H 'Host: example.com'
# 验证已清除
curl -sI http://127.0.0.1/ -H 'Host: example.com' | grep X-Cache
# X-Cache-Status: MISS
工作原理
flowchart LR
A["👤 访客"] -->|"GET /post/"| B["NGINX"]
B -->|"缓存命中"| C["⚡ 即时响应"]
B -->|"缓存未命中"| D["PHP-FPM"]
D --> E["WordPress"]
E --> D
D -->|"存储在缓存中"| B
B --> C
style A fill:#667eea
style B fill:#009639,color:#fff
style C fill:#48bb78,color:#fff
style D fill:#4a90d9,color:#fff
style E fill:#4a90d9,color:#fff
性能比较
| 指标 | 无缓存 | FastCGI 缓存 |
|---|---|---|
| 响应时间 | 200-500ms | 5-20ms |
| 请求/秒 | 50-100 | 5,000+ |
| CPU 使用率 | 高 | 最小 |
| 内存 | 高(PHP) | 低(NGINX) |
实际影响
FastCGI 缓存通常将 WordPress 的响应时间提高 10-50倍,并且能够轻松处理流量高峰。
高级配置
按内容类型缓存不同的持续时间
# 缓存页面 60 分钟
fastcgi_cache_valid 200 60m;
# 缓存重定向 10 分钟
fastcgi_cache_valid 301 302 10m;
# 不要长时间缓存错误
fastcgi_cache_valid 404 1m;
fastcgi_cache_valid 500 502 503 504 0;
在错误期间提供过期内容
fastcgi_cache_use_stale error timeout invalid_header
updating http_500 http_502 http_503 http_504;
缓存锁以防止冲击
fastcgi_cache_lock on;
fastcgi_cache_lock_timeout 5s;
当多个请求同时访问未缓存的页面时,只有一个请求会发送到 PHP-FPM。其他请求会等待缓存填充。
故障排除
页面未被缓存
检查 $skip_cache 变量。常见问题:
- 所有 URL 上的查询字符串(某些插件会在页面上添加
?ver=) - 所有页面上都设置了 cookies
- 检查:
curl -sI your-url | grep Set-Cookie
登录页面被缓存
验证 cookie 检查是否包括您的认证 cookies:
if ($http_cookie ~* "wordpress_logged_in") {
set $skip_cache 1;
}
缓存未清除
- 验证插件设置:自定义 IP =
127.0.0.1 - 确保缓存后端设置为 NGINX(或在
wp-config.php中定义了VHP_PURGE_BACKEND) - 手动测试:
curl -X PURGE 'http://127.0.0.1/page/*'