昨天下午没有太多事情,想起来一直想弄的nginx fastcgi_cache还没有弄,于是趁着重装景安服务器的功夫,顺便把这个玩意也搞搞,弄到晚上7点多,走了不少弯路,不过最后总算是搞定了。
方法步骤如下:
一、安装Nginx ngx_cache_purge模块
由于我使用的是oneinstack面板,所以以下的步骤都以此为基础,其他比如lnmp没有试过,可能需要微调一下。
# nginx -V 2>&1 | grep -o ngx_cache_purge 查看ngx_cache_purge是否安装,没有数据表示未安装
cd /root/oneinstack/src
wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
tar xzf ngx_cache_purge-2.3.tar.gz
#以下几个安装包都是Oneinstack自带的,不同的版本可能会不同,请根据情况调整
tar xzf nginx-1.16.1.tar.gz
tar xzf pcre-8.43.tar.gz
tar xzf openssl-1.1.1c.tar.gz
cd /root/oneinstack/src/nginx-1.16.1
nginx -V #查看nginx编译参数,最后加上--add-module=../ngx_cache_purge-2.3
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --with-openssl=../openssl-1.0.2o --with-pcre=../pcre-8.42 --with-pcre-jit --with-ld-opt=-ljemalloc --add-module=../ngx_cache_purge-2.3
make #编译
mv /usr/local/nginx/sbin/nginx{,_`date +%F`} #备份nginx
cp objs/nginx /usr/local/nginx/sbin
nginx -V 2>&1 | grep -o ngx_cache_purge
# 显示ngx_cache_purge表示已经安装成功、
使用Nginx -V
查看编译参数添加add-module时,一定要根据你自己的Nginx的编译参数来操作,也就是说保留原来的Nginx参数再加上add-module。例如我的:
二、Nginx开启fastcgi_cache缓存-配置实例
这里我直接贴出我的www.yanjingweb.cn的配置实例
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;
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;
}
#这里请参考你网站之前的配置,特别是sock的路径,弄错了就502了!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 Cache-Control max-age=0;
add_header Nginx-Cache "$upstream_cache_status";
add_header Last-Modified $date_gmt;
add_header X-Frame-Options SAMEORIGIN; # 只允许本站用 frame 来嵌套add_header X-Content-Type-Options nosniff; # 禁止嗅探文件类型add_header X-XSS-Protection "1; mode=block"; # XSS 保护etag on;
fastcgi_cache WORDPRESS;
fastcgi_cache_valid 200 301 302 1d;
}
#缓存清理配置(可选)location ~ /purge( /.*) { #为防止转义,请去掉{ /之间的空格allow 127.0.0.1;
#此处填写你的服务器IPallow 122.115.122.111;
deny all;
#请注意此处的WORDPRESS要与上面的keys_zone保持一致fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
}
以下略
至此,全部设置结束,nginx -t 检查以下nginx。然后重新启动一下nginx。service nginx restart
用浏览器打开一个网页,F5刷新几下,看看浏览器的head,就能看到nginx_cache hit字样,说明缓存成功了。然后看缓存文件夹了,也有了一堆的文件。
最后,说一下
开通这个之后,确实打开网页的速度快了不少,在不用CDN的前提下,网页也能秒开了,有动手能力的同学不妨一试。
速度还是挺快的