WordPress开启Nginx fastcgi_cache缓存加速,这是一篇转载并整理的文章,只为备忘,文章内容大部分来源于张戈博客。
在希望使用Nginx缓存前需要查看一下你的Nginx是否编译了ngx_cache_purge 模块,如果没有则需要编译之
查看ngx_cache_purge是否安装
nginx -V 2>&1 | grep -o ngx_cache_purge
显示ngx_cache_purge表示已经安装,如果没有,则需要重新编译Nginx,在原有的编译参数上新增一个 ngx_cache_purge 模块,比如:
–add-module=../ngx_cache_purge-2.3
编译完后,进行Nginx配置
要用这个缓存功能,建议重新弄一个 server 模块(替换之前的)
#下面2行的中的wpcache路径请自行提前创建,否则可能会路径不存在而无法启动nginx,max_size请根据分区大小自行设置
# 其实你还可以把fastcgi_cache_path缓存路径设置在tmpfs内存中,操作系统不同tmpfs路径也不同,如下:CentOS:/dev/shm Ubuntu和Debian:/run/shm
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里面,避免加了多次。
server
{
listen 80;
#请修改为自己的域名
server_name zhang.ge;
index index.html index.htm index.php default.html default.htm default.php;
#请修改为自己网站的存放路径
root /home/wwwroot/zhang.ge;
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;
}
#这里请参考你网站之前的配置,特别是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 / {
#此处可以添加自定义的伪静态规则(之前你新增的伪静态规则可以添加到这,没有就不用了)
try_files $uri $uri/ /index.php?$args;
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
}
#缓存清理配置(可选模块,请细看下文说明)
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; }
#请注意修改日志路径
access_log /home/wwwlogs/zhang.ge.log access;
}
在wordpress中,还一个专门为WordPress量身定做的缓存清理插件:Nginx Helper。下面是部分功能说明
清理模式建议选择模式二(Delete local server cache files),不过由于插件作者定义的缓存路径是 /var/run/nginx-cache ,而我们可能会根据服务器实际情况来自定义缓存路径,这样一来,缓存路径的不同就会导致插件无法找到缓存文件并删除!
解决办法:
很简单,在WordPress根目录下的wp-config.php中新增如下代码即可:
//根据实际情况定义缓存的存放路径
define( ‘RT_WP_NGINX_HELPER_CACHE_PATH’,’/tmp/wpcache’);
Ps:不知道添加到第几行的话,可以添加到 define(‘WPLANG’, ‘zh_CN’); 的后面即可。添加后建议重载一下php,确保变量生效(主要针对开启了PHP缓存的网站)。
实际使用过程中,很多人还是有这样那样的问题,主要的问题就是
1、移动端缓存和PC端缓存混肴,导致某些wordpress在相应端显示错误!
2、某些页面我不想缓存
其实,根据上面的代码很简单!
针对问题1,张戈在评论也有介绍,主要是要去百度或者Google下Nginx对移动端的判断,下面是我整理出的,可能不完整,大家自己补充:
if ($http_user_agent ~* “Android|webOS|iPhone|iPod|BlackBerry|IEMoble|Mobile|Tablet”) {
set $skip_cache 1;
}
针对问题2,其实注意看张戈提供的代码
#后台等特定页面不缓存(其他需求请自行添加即可)
if ($request_uri ~* “/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml”) {
set $skip_cache 1;
}
已经很明确了,比如我友情链接页面不想缓存,就加上
#后台等特定页面不缓存(其他需求请自行添加即可)
if ($request_uri ~* “/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml|links.html”) {
set $skip_cache 1;
}
附录:fastcgi_cache配合timthumb.php缩略图组件直接缓存到本地。
首先,自然是要先按上面教程配置好fastcgi_cache,然后在Nginx配置中添加Nginx重写PHP缩略图URL规则
#Nginx重写PHP缩略图URL规则
location ~ .*\.(gif|jpg|jpeg|png|bmp)$ {
set $width ”;
set $height ”;
set $width $arg_w;
set $height $arg_h;
#只要图片带上宽度(?w=)或高度(?h=)参数,都会将访问重写到/thumb/timthumb.php?src=* 这个动态缩略图生成的接口上
if ( $width != ” ) {
rewrite ^(.*)$ /thumb/timthumb.php?src=http://$host/$1 last;
}
if ( $height != ” ) {
rewrite ^(.*)$ /thumb/timthumb.php?src=http://$host/$1 last;
}
expires max;
}
如果你只想缓存timthumb.php生存的缩略图,那么在上面的Nginx配置中“location ~ [^/]\.php(/|$) { ” 这段上面加入
location ~* /timthumb.php { #指定仅timthumb.php使用fastcgi_cache缓存
try_files $uri =404;
fastcgi_pass unix:/tmp/php-73-cgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $DOCUMENT_ROOT$fastcgi_script_name;
fastcgi_param PATH_INFO $2;
include fcgi.conf;
add_header X-Cache “$upstream_cache_status From $host”;
fastcgi_cache WORDPRESS;
fastcgi_cache_valid 200 301 302 1d;
}