fastcgi_cache
是 Nginx 自带的缓存功能之一,可以缓存fastcgi
生成的内容,很多情况是 php 生成的动态的内容。简单来说就是将动态页面缓存到内存或者硬盘上,如果符合条件则直接读取缓存,不再与 PHP 通信,无论是速度还是性能提升到非常明显。
虽然 Nginx 内置了fastcgi_cache
,但是并没有 Purge 功能,我们需要重新编译 Nginx 来添加fastcgi_cache_purge
模块,开源的fastcgi_cache_purge
只支持单一 Key 删除,如果想分组删除只能自己开发了。
检查是否安装fastcgi_cache_purge
nginx -V 2>&1 | grep nginx_cache_purge -o
一般都不会安装,如果显示ngx_cache_purge
则已安装。
下载 Nginx 安装包,可选稳定版和最新版。
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
解压压缩包
tar xzf nginx-1.13.9.tar.gz
tar xzf ngx_cache_purge-2.3.tar.gz
进入目录
cd nginx-1.13.9
查看你的 nginx 配置参数
nginx -V
在你的参数后面加上--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-ipv6 --with-http_gzip_static_module --with-http_realip_module --with-http_flv_module --with-openssl=../openssl-1.0.2h --with-pcre=../pcre-8.39 --with-pcre-jit **--add-module=../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 -V 2>&1 | grep ngx_cache_purge -o
若显示ngx_cache_purge
则表示已经安装成功。
这里我们将fastcgi_cache_path
设置tmpfs
内存中,这样比放在硬盘上更加快。CentOS 的目录在**/dev/shm**
使用df -h /var/run
命令查看空间大小
修改你的网站配置文件,下面为实例
#如果缓存多站点则把下面四行放到nginx.conf 中
fastcgi_cache_path /var/run/nginx-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 {
server_name example.com www.example.com;
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
root /var/www/example.com/htdocs;
index index.php;
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;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
fastcgi_cache WORDPRESS;
fastcgi_cache_valid 60m;
}
location ~ /purge(/.*) {
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; }
}
之后直接搜索Nginx Helper
插件,安装激活即可。
使用了fastcgi_cache
后,可以使用upstream_cache_status
来检测缓存状态。
在nginx.conf
的http {..}
中加入下面的参数
add_header rt-Fastcgi-Cache $upstream_cache_status;
之后执行service nginx reload
即可
之后查看 HTTP 的相应头部,如果看到下面的参数则命中缓存。
rt-Fastcgi-Cache: HIT
MISS 代表没有找到缓存
BYPASS 代表跳过缓存
EXPIRED 代表缓存过期
以上。
共有 条评论