便宜VPS主机精选
提供服务器主机评测信息

WordPress开启Nginx高速低耗fastcgi_cache缓存加速方案完整版

之前,笔者分享了一篇WordPress加速之Nginx FastCGI Cache PK Varnish的博文,并明确表示会分享WordPress开启Nginx FastCGI Cache缓存的方法。

不过,因笔者一直懒得弄长篇大论,因此一直未实现诺言。今天,还是抽空完成,希望帮到那些想折腾的朋友。
一、安装Nginx ngx_cache_purge模块

具体安装ngx_cache_purge模块教程可以去 https://www.vpsrr.com/lnmp-install-ngx_cache_purge/ 看看。

这里特别提醒下,对照lnmp的版本,路径和参数稍微有点不一样。
二、WordPress开启nginx fastcgi_cache缓存配置实例

这里贴出xianba.net的Nginx开启fastcgi_cache缓存配置实例,说明如下:

fastcgi_cache_path /home/cache/path levels=1:2 keys_zone=WORDPRESS:128m inactive=1d max_size=10g;
fastcgi_temp_path /home/cache/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;

server {
listen 80;
listen 443 ssl http2;
…………………此部省略……………………

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;
}

location ~ [^/]\.php(/|$) {
#fastcgi_pass remote_php_ip:9000;
fastcgi_pass unix:/dev/shm/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 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”;
fastcgi_cache WORDPRESS;
fastcgi_cache_valid 200 301 302 1d;
}

#缓存清理配置(可选)
location ~ /purge( /.*) { #为防止转义,请去掉{ /之间的空格
allow 127.0.0.1;
allow xxx.xxx.xxx.xxx;
deny all;

fastcgi_cache_purge WORDPRESS “$scheme$request_method$host$1”;
}
…………………此部分省略……………………

}

几个常见问题解答。

1、缓存是存到本地还是内存中?

在上面的配置找中,缓存文件会存到fastcgi_cache_path和fastcgi_temp_path目录下,原创文章的作者建议将缓存文件存到内存路径。

例如:/dev/shm/nginx-cache levels=1:2 keys_zone=WORDPRESS:100m inactive=60m;

这里,笔者建议如果您的VPS或杜甫内存大可以存在内存中,如果内存大小一般但硬盘读写速度飞钻石盘完全可以存在硬盘中。

2、nginx fastcgi_cache访问过程是?是否能起到提升降耗的功效?

这里直接看下图:

三、wordpress清除nginx fastcgi_cache缓存的方法

通过上面的方法配置好了wordpress的nginx fastcgi_cache缓存。那么,我们如何自动更新Nginx fastcgi_cache缓存页面了。

这里,笔者推荐使用WordPress程序的Nginx Helper插件,专为wordpress的nginx fastcgi_cache缓存更新而开发的插件。

插件作者默认定义的缓存路径是 /var/run/nginx-cache ,因此需要设置。在 WordPress 根目录下的 wp-config.php 中新增如下代码即可:

define( ‘RT_WP_NGINX_HELPER_CACHE_PATH’,’/home/cache/path’);

四、WordPress Nginx fastcgi_cache缓存生效查看

启用了Nginx fastcgi_cache后,就可以在浏览器Header 头部信息中看到是否已经命中了。因笔者加了百度CDN,就不截图了。

当然,也可用通过服务器的缓存路径中也能看到Nginx fastcgi_cache生成的缓存文件。
五、WordPress Nginx fastcgi_cache缓存小结

WordPress开启nginx fastcgi_cache缓存可用大幅加快网页响应速度,节省服务器资源。

笔者是WP建站的,经过访问量小,不过还是一直用的军哥LNMP环境,并且开启了nginx fastcgi_cache缓存,效果非常明显。

未经允许不得转载:便宜VPS测评 » WordPress开启Nginx高速低耗fastcgi_cache缓存加速方案完整版