WordPress开启NGINX-Fastcgi_cache缓存

WordPress在不做任何优化的情况下,网站加载的速度实在不敢恭维,最好的优化方式就是给WordPress做一个缓存,前面的文章为大家提供了两种WordPress缓存方案,一个是memcached+batcache,一个是redis,这两种缓存任选一个就足以让你的WordPress站点秒加载,感兴趣的可以看下

而本篇文章讲的就是另一个缓存,那就是NGINX-Fastcgi_cache,直接由NGINX来缓存你的网站页面,并且支持伪静态页面,效率更高

安装Nginx ngx_cache_purge模块

宝塔已经默认编译了这个模块,我们可以输入nginx -V来查询是否编译了这个模块,看到ngx_cache_purge就说明是编译成功了的

查看ngx_cache_purge模块是否编译

如果没有使用宝塔就需要到官方网站:http://labs.frickle.com/files/ 里下载最新的ngx_cache_purge模块自行编译

创建缓存文件夹

需要在根目录(服务器根目录)的tmp文件夹里创建wpcache文件夹,用作存储网站缓存的目录

路径:/tmp/wpcache

修改站点vhost文件

我们需要在站点vhost文件里添加一下我们的缓存配置

将以下代码加在server

#下面各个参数的含义请自行百度,我就不赘述了
#下面 2 行的中的 wpcache 路径请自行提前创建,否则可能会路径不存在而无法启动 nginx,max_size 请根据分区大小自行设置
fastcgi_cache_path /tmp/wpcache levels=1:2 keys_zone=WORDPRESS:250m inactive=1d max_size=1G;
fastcgi_temp_path /tmp/wpcache/temp;
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;
#Ps:如果是多个站点,以上内容不要重复添加,否则会冲突,可以考虑将以上内容添加到 nginx.conf 里面,避免加了多次。

将以下代码加入#SSL-END下面

#文章中vhost代码参考文献:https://zhang.ge/5042.html  https://zhang.ge/5067.html #
#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;
        }
        #这里请参考你网站之前的配置,特别是 sock 的路径,弄错了就 502 了!
        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;
        }
        #缓存清理配置(可选模块,请细看下文说明)
        location ~ /purge(/.*) {
            allow 127.0.0.1;
            allow "此处填写你服务器的真实外网 IP";
            deny all;
            fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
        }
    
        location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
                access_log off; log_not_found off; expires max;
        }
        location = /robots.txt { access_log off; log_not_found off; }
        location ~ /\. { deny  all; access_log off; log_not_found off; }

宝塔面板的sock文件在/www/server/php/你的php版本/etc/php-fpm.conf文件中可看到sock文件路径

sock文件路径

WordPress安装Nginx Helper插件

WordPress后台插件市场搜索Nginx Helper即可安装,这个插件可以和fastcgi缓存配合使用,清理缓存更方便,但是插件没有中文翻译,大家可以用浏览器翻译查看,或者对照下面我标注的插件界面使用

Nginx  Settings插件界面
Nginx  Settings插件界面
Nginx  Settings插件界面

缓存清理选项建议大家使用本地清理模式,因为用purge清理方式会产生请求

启用Enable Nginx Timestamp in HTML这个插入缓存信息以后,打开你的缓存页面查看源代码底部将会有类似以下信息

<!--Cached using Nginx-Helper on 2023-08-10 13:59:57. It took 1 queries executed in 0.161 seconds.-->

由于插件默认的缓存路径为 /var/run/nginx-cache ,它找不到我们设置的路径,所以插件清理缓存的效果就没有用,我们需要在wp-config.php中加入以下代码定义插件缓存路径即可

//根据实际情况定义缓存的存放路径
define( 'RT_WP_NGINX_HELPER_CACHE_PATH','/tmp/wpcache');

不知道放在哪里的话就放在define( 'WP_DEBUG', false );下方即可

查看缓存效果

成功启用缓存效果后我们查看网站header信息会有一个x-cache的信息,x-cache有3种状态

  • HIT 表示缓存命中
  • MISS 表示缓存未命中 这个页面还没被缓存 刚发布内容首次访问状态
  • BYPASS 表示缓存黑名单 登录管理后台再访问网站就是这个状态
缓存命中状态

注意事项

缓存清理直接到Nginx Helper插件处点击红按钮Purge Entire Cache即可,如果清不掉可能是前面叫加在wp-config.php里的代码漏加了,也可以直接把wpcache文件夹里的文件全部删除也可清空缓存。

有的CDN接入后x-cache会一直显示BYBASS状态,不用担心,这是正常的,缓存依然是生效的

多站点使用fastcgi_cache解决方法

直接将以下代码加入nginx的vhost文件

#站点 1 缓存配置
fastcgi_cache_path /tmp/a_cache levels=1:2 keys_zone=www.a.com:384m inactive=1d max_size=5G;
#站点 2 缓存配置
#如果要开启更多站点缓存,请继续增加,注意每个站点的 缓存路径 和 keys_zone 要自定义区分一下
#Ps:代码中的参数都只是范例,实际使用请根据服务器配置自行修改
fastcgi_cache_path /tmp/b_cache levels=1:2 keys_zone=www.b.com:384m inactive=1d max_size=5G;
#其他配置可以不变
fastcgi_temp_path /tmp/temp_cache;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;

要将站点vhost文件中含以上的代码剔除掉再添加,文件夹同样需要自行添加

下面是两个示例站点的vhost文件示例,把fastcgi_cache后面的值填为自己的网站域名即可

a站点vhost文件示例

server(
#其他配置略 
location ~ [^/]\.php(/|$)
            {
                try_files $uri =404;
                fastcgi_pass  unix:/dev/shm/php-cgi.sock;
                fastcgi_index index.php;
                include fastcgi.conf;
                #fastcgi 缓存配置
                fastcgi_cache_bypass $skip_cache;
                fastcgi_no_cache $skip_cache;
                add_header X-Cache "$upstream_cache_status From $host";
                fastcgi_cache www.a.com;
                fastcgi_cache_valid 200 301 302 1d;
        }
#其他配置略
}

b站点vhost文件示例

server(
#以上配置略 
location ~ [^/]\.php(/|$)
            {
                try_files $uri =404;
                fastcgi_pass  unix:/dev/shm/php-cgi.sock;
                fastcgi_index index.php;
                include fastcgi.conf;
                #fastcgi 缓存配置
                fastcgi_cache_bypass $skip_cache;
                fastcgi_no_cache $skip_cache;
                add_header X-Cache "$upstream_cache_status From $host";
                fastcgi_cache www.b.com;
                fastcgi_cache_valid 200 301 302 1d;
        }
#以下配置略
}

这样配置多站点配置fastcgi缓存就没问题了

版权声明:
文章标题:WordPress开启NGINX-Fastcgi_cache缓存
文章作者:HONG
文章链接:https://www.hongnote.cn/100.html
CC共享协议许可协议:BY-NC-SA 4.0
THE END
分享
二维码
打赏
< <上一篇
下一篇>>
文章目录
关闭
目 录