之前写过一个基于wordpress缓存插件的页面缓存方案,着实能起到很好的效果,但使用一段时间后发现使用插件缓存效率较低,BUG较多,随后查阅资料找到了一种基于nginx+fastcgi的服务器级别的缓存方案,效果特别好,这种方案由于直接绕过PHP进行缓存,效率更高。
而且宝塔预设了wordpress的fastcgi缓存策略!我想很多人都不知道,我是今天去设置的时候才发现的(bushi)。
原理
页面缓存插件比如W3 Total Cache、WP Super Cache等都是经过NGINX——PHP——WP缓存插件——本地或对象缓存这样一种流程,而fastcgi缓存则直接使用NGINX——fastcgi缓存,提高了效率。
nginx的fastcgi cache是用来缓存用户请求,当用户下次再进行同样的访问的时候直接将缓存结果返回给用户,避免了nginx再向上游请求结果的过程,使服务性能大幅度提升,如果服务是静态可缓存的话使用这个模块能够明显缩短用户请求时间同时节省服务器资源,大大提升服务的qps。
基于宝塔面板(aapanel)设置

宝塔默认的配置文件里面有:
1 2 3 4 5 6
| fastcgi_cache_key "$scheme$request_method$host$request_uri"; fastcgi_cache_path /dev/shm/nginx-cache/wp levels=1:2 keys_zone=WORDPRESS:100m inactive=60m max_size=1g; fastcgi_cache_use_stale error timeout invalid_header http_500; fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
|
并且通过df -h查看服务器磁盘挂载情况发现tmpfs 2.0G 712K 2.0G 1% /dev/shm,还是一个高速内存的缓存盘!
站点设置
在站点配置文件中找到:
1 2 3
| include enable-php-74.conf;
|
改成:
1 2 3
| #PHP-INFO-START PHP reference configuration, allowed to be commented, deleted or modified include enable-php-74-wpfastcgi.conf; #PHP-INFO-END
|
接着编辑配置文件:
1 2 3 4 5 6 7 8 9 10
| nano /www/server/nginx/conf/enable-php-74-wpfastcgi.conf
location ~ /purge(/.*) { allow 127.0.0.1; allow yourip; deny all; fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1"; }
|
重启nginx,
用无痕浏览打开你的网站,然后F12打开开发者工具,
找到:

第一次打开可能是MISS,再刷新一次看到HIT from your.domain就是成功!
配置自动缓存刷新
安装Nginx Helper:

配置:

保存即可。
如果想要设置定时刷新,可以使用crontab:
1
| find /dev/shm/nginx-cache/wp -type f -delete
|
非宝塔配置
其实很简单,nginx主配置文件加入:
1 2 3 4
| fastcgi_cache_key "$scheme$request_method$host$request_uri"; fastcgi_cache_path /dev/shm/nginx-cache/wp levels=1:2 keys_zone=WORDPRESS:100m inactive=60m max_size=1g; fastcgi_cache_use_stale error timeout invalid_header http_500; fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
|
站点配置文件加入:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| 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-74.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"; fastcgi_cache WORDPRESS; 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_valid 200 301 302 1d; }
location ~ /purge(/.*) { allow 127.0.0.1; allow your ip; deny all; fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1"; }
|
重启nginx即可使用。