Nginx_fastcgi_cache 静态化

记录

Oneinstack 一键脚本基础上直接安装 nginx_cache_purge 模块,其它方式无差别。

cd /root/lnmp/src 
wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
wget http://nginx.org/download/nginx-1.11.1.tar.gz
tar xzf ngx_cache_purge-2.3.tar.gz
tar xzf nginx-1.11.1.tar.gz
cd nginx-1.11.1

nginx -V查看 nginx 编译参数

--prefix=/wp-content/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_v2_module --with-http_ssl_module --with-ipv6 --with-http_gzip_static_module --with-http_realip_module --with-http_flv_module --with-ld-opt=-ljemalloc

编译参数前加上:./configure 后面加:--add-module=../ngx_cache_purge-2.3

举例:

./configure --prefix=/wp-content/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_v2_module --with-http_ssl_module --with-ipv6 --with-http_gzip_static_module --with-http_realip_module --with-http_flv_module --with-ld-opt=-ljemalloc --add-module=../ngx_cache_purge-2.3

开始编译

make
mv /wp-content/local/nginx/sbin/nginx{,_`date +%F`}   # 备份 nginx
cp objs/nginx /wp-content/local/nginx/sbin
nginx -V 2>&1 | grep -o ngx_cache_purge # 显示 ngx_cache_purge 表示已经安装成功。

修改虚拟主机文件:/wp-content/local/nginx/conf/vhost/xxx.com.conf

# 假设设定缓存路径/home/cache,10m 为占用内存
fastcgi_cache_path /home/cache levels=1:2 keys_zone=WORDPRESS:100m inactive=60m;
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;
server_name website.com www.website.com;
access_log /home/wwwlogs/website.com_nginx.log combined;
index index.html index.htm index.php;
include wordpress.conf;
root /home/wwwroot/website;

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 unix:/run/shm/php-cgi.sock;
 fastcgi_index index.php;
 include fastcgi.conf;
 # 缓存规则
 fastcgi_cache_bypass $skip_cache;
 fastcgi_no_cache $skip_cache;
 fastcgi_cache WORDPRESS;
 # 缓存过期
 fastcgi_cache_valid 200 301 302 30m;
}

# 清除缓存区域
location ~ /purge(/.*) {
 allow 127.0.0.1;
 deny all;
 fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
 }  

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
 expires 30d;
 access_log off;
 }
location ~ .*\.(js|css)?$ {
 expires 7d;
 access_log off;
 }
}

最后保存,重启 service nginx reload

缓存插件

1.安装插件:Nginx Helper
2.默认清理路径:/var/run/nginx-cache,手动设定路径为:/home/cache
3.修改 wp-config.php 添加路径:define( 'RT_WP_NGINX_HELPER_CACHE_PATH','/home/cache');

设置

根据自己情况调整。

效果

勾选 Enable Nginx Timestamp in HTML,在网页源码底部看到缓存调试信息:

清除缓存

自动:设定好更新文章或者提交评论自动清除当页缓存。
手动:后台点击 Purge Cache 即可清空所有/home/cache 缓存。

本文根据站点实际使用总结并修改。

2017-5-11 更新 Typecho for phphao:

nginx 配置

# http
fastcgi_cache_path /data/cache levels=1:2 keys_zone=Typecho:50m inactive=10m;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;

# server
fastcgi_cache Typecho;
fastcgi_cache_valid 200 302 301 1h;
fastcgi_cache_valid any 1m;
fastcgi_cache_min_uses 1;
fastcgi_cache_methods GET HEAD;
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_cache_key $request_method://$host$request_uri;

参考

WordPress 优化——利用 Nginx fastcgi_cache 缓存加速
Nginx + WordPress + Fastcgi_Cache 自动清空缓存
Nginx 开启 fastcgi_cache 缓存加速,支持 html 伪静态页面