看着大发的教程折腾了一番,看样子是成功了。2019 已重新配置上整站缓存。
简介:Fastcgi_Cache 是 Nginx 自带缓存功能之一,可以缓存 fastcgi 生成的内容,很多情况是 php 生成的动态的内容。简单来说就是将动态页面缓存到内存或者硬盘上,如果符合条件则直接读取缓存,不再与 php 通信,无论是速度还是性能提升到非常明显。
环境:搬瓦工 Centos 7 x86_64 bbr + LNMP
检查是否已安装 fastcgi_cache_purge
nginx -V 2>&1 | grep -o ngx_cache_purge
一般都不会安装,如果显示 ngx_cache_purge 则已安装。
编译安装 nginx_cache_purge
下载 Nginx 和 ngx_cache_purge 安装包,可选稳定版或者最新版。
/root/lnmp1.5/src
wget --no-check-certificate -c http://nginx.org/download/nginx-1.13.9.tar.gz
wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
# ngx_cache_purge 备用下载地址
wget --no-check-certificate -c -O ngx_cache_purge-2.3.tar.gz https://github.com/FRiCKLE/ngx_cache_purge/archive/2.3.tar.gz
解压
tar xzf nginx-1.13.9.tar.gz
tar xzf ngx_cache_purge-2.3.tar.gz
进入目录 nginx-1.13.9
cd nginx-1.13.9
查看现有 nginx 配置参数
nginx -V
在现有的参数后面加上 --add-module=../ngx_cache_purge-2.3,参考如下
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_sub_module --with-stream --with-stream_ssl_module --with-openssl=/root/lnmp1.5/src/openssl-1.1.1-dev --with-openssl-opt='enable-tls1_3 enable-weak-ssl-ciphers' --add-module=/root/lnmp1.5/src/ngx_cache_purge-2.3
注意:如果你安装了openssl和pcre 等模块也要下载相应的压缩包。
开始编译,注意不要make install,只需要 make
make
备份原来的 Nginx编译文件,文件名称为 nginx_2018-xx-xx
mv /usr/local/nginx/sbin/nginx{,_`date +%F`}
拷贝新的编译文件过去
cp objs/nginx /usr/local/nginx/sbin/nginx
检查配置
/usr/local/nginx/sbin/nginx -t
升级
make upgrade
检查是否安装成功
/usr/local/nginx/sbin/nginx -v # 检查 Nginx 版本
nginx -V 2>&1 | grep -o ngx_cache_purge
若显示 ngx_cache_purge 则表示已经安装成功。
Nginx 配置
建议将 fastcgi_cache_path 放到 tmpfs 内存中,比放在硬盘上更加快。CentOS 目录在 /dev/shm,Ubuntu 和 Debian 在 /run/shm。使用 df -h /var/run 命令查看空间大小。
创建缓存目录
比如,在 run/ 目录下创建 nginx-cache
mkdir -p /var/run/nginx-cache
修改网站配置文件
下面为实例,蓝色字体部分请根据实际修改。
#如果缓存多站点则把下面四行放到nginx.conf 中
fastcgi_cache_path /var/run/nginx-cache levels=1:2 keys_zone=WORDPRESS:100m inactive=1d;
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 443 ssl http2;
#listen [::]:443 ssl http2;
server_name cyhour.com www.cyhour.com;
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/cyhour.com;
ssl on;
ssl_certificate /usr/local/nginx/conf/ssl/cyhour.com.crt;
ssl_certificate_key /usr/local/nginx/conf/ssl/cyhour.com.pem;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_ciphers "TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256:EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+ECDSA+AES128:EECDH+aRSA+AES128:RSA+AES128:EECDH+ECDSA+AES256:EECDH+aRSA+AES256:RSA+AES256:EECDH+ECDSA+3DES:EECDH+aRSA+3DES:RSA+3DES:!MD5";
ssl_session_cache builtin:1000 shared:SSL:10m;
# openssl dhparam -out /usr/local/nginx/conf/ssl/dhparam.pem 2048
ssl_dhparam /usr/local/nginx/conf/ssl/dhparam.pem;
add_header Strict-Transport-Security "max-age=31536000; includeSubdomains; preload";
include wordpress.conf;
#error_page 404 /404.html;
# Deny access to PHP files in specific directory
#location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }
set $skip_cache 0;
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;
}
#include enable-php.conf;
location ~ [^/]\.php(/|$) {
try_files $uri =404;
#fastcgi_pass remote_php_ip:9000;
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 rt-Fastcgi-Cache "$upstream_cache_status From $host";
fastcgi_cache WORDPRESS;
fastcgi_cache_valid 200 301 302 1d;
}
location ~ /purge(/.*) {
allow 127.0.0.1;
allow 此处填写服务器外网IP地址;
deny all;
fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location ~ /.well-known {
allow all;
}
location ~ /\.
{
deny all; access_log off; log_not_found off;
}
include custom.conf;
access_log /home/wwwlogs/cyhour.com.log main;
}
使 nginx 配置生效
service nginx reload
安装 Nginx Helper 插件
Nginx 内置 FastCgi 缓存,但是不支持自动清除缓存。当你在 WordPress 里面新建/修改一篇文章,或者访客提交评论的时候,就需要手动清理缓存!安装为 ngx_cache_purge 量身定做的 WordPress 缓存清理插件:Nginx Helper 可以解决这个问题。
WordPress 后台搜索 Nginx Helper 插件,安装激活,使用默认配置即可。
检查 nginx_cache_purge 是否正常工作
使用了 fastcgi_cache 后,可以使用 upstream_cache_status 来检测缓存状态。
在 nginx.conf 的 http {..} 中加入下面的参数(或者在网站配置文件中 server {...} 中加入)
add_header rt-Fastcgi-Cache $upstream_cache_status;
上面网站配置中已添加。
然后执行 service nginx reload
查看 HTTP 的响应头部,可以看到 rt-Fastcgi-Cache 状态。
HIT 代表命中缓存 MISS 代表没有找到缓存 BYPASS 代表跳过缓存 EXPIRED 代表缓存过期
还可以刷新站点,看缓存目录是否增大。
du -sh /var/run/nginx-cache
参考资料
大发 - 使用 Nginx 中的 fastcgi_cache 为 WordPress 缓存加速
LinuxEye - WordPress 优化——利用 Nginx fastcgi_cache 缓存加速
温馨提示:开启fastcgi_cache需谨慎!!!上线前需先测试,修改主题解决以下两个问题:1.fastcgi_cache 可能会缓存评论者名称和邮箱信息;2.如果在登陆状态回复评论,会缓存登陆界面。
我好像没有折腾这个,不过目前速度还行,以后有需要再折腾
@懿古今 我是纯粹折腾…
我也折腾上了,我那破网站就1核 1G 40G,就跑一个站有点废了,用的是tengine,感觉不到啥呢~~
@夜枫 我都是一键包安装的,我这破站才 512M 内存呢。
@老头 你们都太能折腾了,。路过
强悍!
这玩意让我来折腾,我选择加CDN。。。
@姜辰 CDN 不好玩。
本来想折腾一下呢,看有这些弊端 还是算了,目前速度还行
@中意眼镜 折腾这个确实要注意。