互联网技术 · 2023年11月23日 0

优化WordPress网站性能的Nginx缓存配置

我在腾讯云上搭建的网站使用的是LNMP一键包,因此我开始研究Nginx的ngx_cache_purge缓存模块。需要注意的是,如果你使用的是宝塔面板,这篇教程可能不适用。目前,我的建站笔记中不再使用这个方法,而是采用了WP Rocket和Redis缓存。

安装Nginx的ngx_cache_purge模块

LNMP一键包默认并没有安装ngx_cache_purge模块,因此需要手动安装。首先,通过SSH连接到VPS,然后执行以下命令:

wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
tar xzf ngx_cache_purge-2.3.tar.gz
cd lnmp1.5

接下来,编辑lnmp.conf文件,在Nginx_Modules_Options参数的引号中添加–add-module=/root/ngx_cache_purge-2.3。之后,执行以下命令以升级Nginx,这样就会自动安装ngx_cache_purge模块:

./upgrade.sh nginx

配置网站的conf文件

lnmp的默认配置文件位于/usr/local/nginx/conf/vhost/目录下,打开你自己的网站配置文件,例如/usr/local/nginx/conf/vhost/blog.naibabiji.com.conf。将以下内容添加到配置文件中:

#设置fastcgi缓存路径 levels代表目录层级,1:2会生成16*256,2:2会生成256*256 keys_zone代表缓冲区名称 inactive代表过期时间 max_size代表最多用多少磁盘空间
fastcgi_cache_path /tmp/nginx-cache levels=1:2 keys_zone=WORDPRESS:250m inactive=1d max_size=1G;
#fastcgi 缓存临时目录
fastcgi_temp_path /tmp/nginx-cache/temp;
#定义fastcgi缓存的key
fastcgi_cache_key "$scheme$request_method$host$request_uri";
#定义哪些情况下可以用过期缓存
fastcgi_cache_use_stale error timeout invalid_header http_500;
#忽略nocache申明
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

server {
    listen 80;
    server_name blog.naibabiji.com;
    return 301 https://blog.naibabiji.com$request_uri; 
}

server {
    listen 443 ssl http2;
    server_name #你的域名;
    index index.html index.htm index.php default.html default.htm default.php;
    root #你的网站目录;
    ssl on;
    set $skip_cache 0;
    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;
    }
    location ~ [^/].php(/|$)
    {
        try_files $uri =404;
        fastcgi_pass unix:/tmp/php-cgi.sock;
        fastcgi_index index.php;
        include fastcgi.conf;
        add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
        fastcgi_cache_bypass $skip_cache;
        fastcgi_no_cache $skip_cache;
        add_header X-Cache "$upstream_cache_status From $host";
        add_header Cache-Control max-age=0;
        add_header Nginx-Cache "$upstream_cache_status";
        add_header Last-Modified $date_gmt;
        add_header X-Frame-Options SAMEORIGIN; 
        add_header X-Content-Type-Options nosniff; 
        add_header X-XSS-Protection "1; mode=block"; 
        etag on;
        fastcgi_cache WORDPRESS;
        fastcgi_cache_valid 200 301 302 1d;
    }
    location ~ /purge(/.*) {
        allow 127.0.0.1;
        allow "你网站外网IP";
        deny all;
        fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
    }
    include rewrite/wordpress.conf;
    include enable-php-pathinfo.conf;
    location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires 30d;
    }
    location ~ .*.(js|css)?$
    {
        expires 12h;
    }
    location ~ /.well-known {
        allow all;
    }
    location ~ /. {
        deny all;
    }
    access_log /home/wwwlogs/blog.naibabiji.com.log;
}

保存配置文件后,重启lnmp。通过Chrome的F12工具检查网站,可以查看缓存状态。在NetWork标签中,按CTRL + R刷新网页,点击名称下的第一个条目,查看右侧代码中的X-Cache状态。通常会有三种状态:MISS(未命中)、HIT(命中)、BYPASS(绕过)。上面的规则设置了登录用户不缓存,因此显示为BYPASS,若使用隐私模式访问则应显示为HIT。

使用Nginx Helper插件自动清理缓存

在后台安装Nginx Helper插件并启动,默认配置保存即可。然后在WordPress的配置文件wp-config.php中添加以下代码:

define(RT_WP_NGINX_HELPER_CACHE_PATH, /tmp/nginx-cache);

再次重启lnmp,并测试游客身份和登录状态下的缓存效果。

Nginx fastcgi_cache缓存加速与WP Super Cache效果对比

我之前使用WP Super Cache和Memcached Object Cache进行缓存加速,测试Nginx fastcgi_cache前特意进行了对比,结果如下图所示:

上面404ms的是Nginx fastcgi_cache的结果,下面455ms的是WP Super Cache的结果,因此Nginx fastcgi_cache的速度略快。不过我最终还是保留了WP Super Cache,因为使用习惯了,速度差异不大,懒得再去折腾新的配置。

参考资料:

  • 挖站否(目前还在用Nginx fastcgi_cache缓存加速,服务器用的搬瓦工,美国机房)
  • 张戈博客(好像现在是用的CloudFlare的CDN加速)

本文是全系列中第12 / 20篇:WordPress优化