博客 / Linux/ 为 WordPress 配置 Nginx FastCGI 缓存与 ngx_cache_purge 模块

为 WordPress 配置 Nginx FastCGI 缓存与 ngx_cache_purge 模块

为 WordPress 配置 Nginx FastCGI 缓存与 ngx_cache_purge 模块

LNMP 环境重新编译 Nginx 以添加 ngx_cache_purge 模块

本文介绍如何在 LNMP 环境中重新编译 Nginx,添加 ngx_cache_purge 模块,并为 WordPress 配置 FastCGI 缓存以实现加速。

检查 ngx_cache_purge 模块状态

首先,通过 SSH 连接到服务器,检查 Nginx 是否已安装 ngx_cache_purge 模块:

nginx -V 2>&1 | grep -o ngx_cache_purge

如果输出 ngx_cache_purge,则表示模块已安装。否则,请继续以下安装步骤。

安装 ngx_cache_purge 模块

以下步骤以 LNMP 1.2 为例,其他版本(如 1.0、1.3)方法类似,请注意将路径中的版本号替换为实际版本。

1. 进入 LNMP 源码目录并下载模块:

cd /root/lnmp1.2-full/src
wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
tar xzf ngx_cache_purge-2.3.tar.gz
cd nginx-1.8.0

2. 查看当前 Nginx 的编译参数:

nginx -V

记录输出的 configure arguments 部分,例如:

configure arguments: --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_spdy_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module

3. 在原有参数基础上添加 ngx_cache_purge 模块,重新配置:

./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_spdy_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module --add-module=../ngx_cache_purge-2.3

4. 编译 Nginx(注意:不要执行 make install):

make

5. 备份原 Nginx 二进制文件:

mv /usr/local/nginx/sbin/nginx{,_$(date +%F)}

6. 复制新编译的二进制文件:

cp objs/nginx /usr/local/nginx/sbin

7. 再次验证模块是否安装成功:

nginx -V 2>&1 | grep -o ngx_cache_purge

输出 ngx_cache_purge 即表示成功。

为 WordPress 配置 FastCGI 缓存

缓存路径说明

为了提高缓存读写速度,建议将缓存目录设置在 tmpfs(内存文件系统)中。tmpfs 具有以下特点:

  • 临时性:数据存储在内存中,服务器重启后丢失。
  • 高速读写:访问速度远快于磁盘。
  • 动态伸缩:按需分配内存空间。

不同系统的 tmpfs 挂载路径:

  • CentOS:/dev/shm/
  • Ubuntu/Debian:/run/shm

修改 Nginx 配置文件

编辑 WordPress 站点的 Nginx 配置文件(通常位于 /usr/local/nginx/conf/vhost/站点名.conf),添加以下配置。

Part 1: 定义缓存路径与参数

# 缓存路径设置(请确保路径存在,否则 Nginx 无法启动)
fastcgi_cache_path /dev/shm/ levels=1:2 keys_zone=WORDPRESS:200m inactive=1d max_size=350M;
fastcgi_temp_path /dev/shm/temp;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;
# 忽略缓存控制头,确保伪静态等被缓存
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

Part 2: 设置缓存跳过条件

set $skip_cache 0;
# POST 请求不缓存
if ($request_method = POST) {
    set $skip_cache 1;
}
# 带查询字符串的请求不缓存
if ($query_string != "") {
    set $skip_cache 1;
}
# 后台、登录页等特定路径不缓存
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
    set $skip_cache 1;
}
# 对已登录用户、评论用户不缓存(可选)
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
    set $skip_cache 1;
}

Part 3: 在 PHP 处理位置启用缓存

location ~ [^/].php(/|$) {
    try_files $uri =404;
    fastcgi_pass unix:/tmp/php-cgi.sock;
    fastcgi_index index.php;
    include fastcgi.conf;
    # 缓存控制
    fastcgi_cache_bypass $skip_cache;
    fastcgi_no_cache $skip_cache;
    add_header X-Cache "$upstream_cache_status From $host";
    fastcgi_cache WORDPRESS;
    fastcgi_cache_valid 200 301 302 1d;
}

Part 4: 配置缓存清理(可选)

location ~ /purge(/.*) {
    allow 127.0.0.1;
    allow 你的服务器公网IP; # 请替换为实际 IP
    deny all;
    fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
}

保存配置文件后,重启 Nginx 使配置生效:

service nginx reload

系统重启后自动创建缓存目录

由于 tmpfs 中的目录在重启后会丢失,需在 Nginx 启动脚本中自动创建。编辑 /etc/init.d/nginx,在 start 部分添加:

if [ ! -d '/dev/shm/你的缓存文件夹' ]; then
    mkdir /dev/shm/你的缓存文件夹
    chown -R ${user}.$user /dev/shm/你的缓存文件夹
fi

WordPress 侧配置

安装 Nginx Helper 插件

在 WordPress 后台安装并启用 Nginx Helper 插件。该插件可自动清理缓存。

修改 wp-config.php

在 WordPress 根目录的 wp-config.php 文件中添加以下定义,指定缓存路径:

define('RT_WP_NGINX_HELPER_CACHE_PATH', '/dev/shm');

完成以上步骤后,重启服务器或 Nginx 服务,FastCGI 缓存即可生效。

  1. avatar
    熊哥club

    很不错!

  2. avatar
    小z博客

    牛哥,换主题了呀。能把友情链接改为https吗?完整地址:https://www.xiaoz.me/ 谢谢。

发表评论

您的邮箱不会公开。必填项已用 * 标注。