添加ngx_cache_purge模块
下载ngx_cache_purge模块
ngx_cache_purge模块的官方地址:http://labs.frickle.com/files/。在这个地址找到最新版的模块版本 ,利用wget下载。

wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gztar zxvf ngx_cache_purge-2.3.tar.gz
我这里利用的便是ngx_cache_purge-2.3。
编译安装ngx_cache_purge模块
利用nginx -V命令查看nginx是否已经安装了这个模块,如果没有安装,须要重新编译安装。
利用军哥lnmp一键安装包的同学,可以在lnmp的安装目录中找到lnmp.conf这个文件,然后在nginx模块中添加ngx_cache_purge。之后重新平滑升级nginx即可。
修正ngxin配置
在利用fastcgi_cache缓存之前,必须先修正nginx配置,详细便是进入虚拟主机配置中,找到domainname.conf,然后修正里面的sever配置。
#下面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里面,避免加了多次。server{listen 80;#请修正为自己的域名server_name zhangge.net;index index.html index.htm index.php default.html default.htm default.php;#请修正为自己网站的存放路径root /home/wwwroot/domainname.com;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/domainname.com.log access;
把稳修正上述代码中的该修正部分,不然nginx重启会出错。当然,如果是启用了https,模块就应相应的改变。
安装Nginx-helper插件
在后台搜索nginx-helper,安装好插件。
关于插件的设置:
如果没有利用CDN就可以选择purge模式,如果利用了CDN最好选择文件模式。
由于插件作者定义的缓存路径是 /var/run/nginx-cache ,而我们可能会根据做事器实际情形来自定义缓存路径,这样一来,缓存路径的不同就会导致插件无法找到缓存文件并删除!
办理的方法:在wp-config.php中增加一行代码:
define( 'RT_WP_NGINX_HELPER_CACHE_PATH','/tmp/wpcache');
这样,就配置好了。