获取更多博主的动态,欢迎加入我的 Telegram 频道:Fatesinger

使用Nginx 中的 fastcgi_cache 为WordPress 缓存加速

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_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

注意,如果你安装了opensslpcre 等模块也要下载相应的压缩包。

开始编译,注意不要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 配置

修改你的网站配置文件,下面为实例

#如果缓存多站点则把下面四行放到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 插件,安装激活即可。

查看nginx_cache_purge是否正常工作

使用了fastcgi_cache后,可以使用upstream_cache_status来检测缓存状态。

nginx.confhttp {..}中加入下面的参数

add_header rt-Fastcgi-Cache $upstream_cache_status;

之后执行service nginx reload即可

之后查看HTTP 的相应头部,如果看到下面的参数则命中缓存。

rt-Fastcgi-Cache: HIT

MISS 代表没有找到缓存

BYPASS 代表跳过缓存

EXPIRED 代表缓存过期

以上。

Responses

  1. 九分裤的头像

    很少能在博主博客看到这类的文章了。

  2. MG的头像

    技术发哥又回来了 :oops:

  3. Tony的头像

    发哥很少写技术了啊

  4. kn007的头像

    :twisted: 没有我的名字

    1. bigfa的头像

      @kn007 评论里不是有了吗

    2. kn007的头像

      @bigfa pia

  5. yalv的头像

    :mrgreen: 技术文,学习学习

  6. god的头像

    这个是不是评论页面不能自动刷新啊 :wink:

    1. bigfa的头像

      @god 安装最后的插件即可

  7. 老杨的头像

    nginx -V 2>&1 | grep nginx_cache_purge -o 应该是 nginx -V 2>&1 | grep ngx_cache_purge -o 吧? :idea:

    1. bigfa的头像

      @老杨 没错,这个要修改 :cry:

  8. 夜枫的头像

    我测试了下,tenginx 2.2居然有此模块,后续好好研究下! :lol: :lol:

    1. bigfa的头像

      @夜枫 不是啥新功能,tengine 也是基于nginx 的。

  9. chancat的头像

    这个..重启后就会丢失的把 老铁?

    1. bigfa的头像

      @chancat 本来就会过期,丢失算什么,缓存失效会重新生成的。

    2. chancat的头像

      @bigfa 我还是挂载重启生效吧 :neutral: :twisted: 发哥,群里都不说话,还以为凉凉了呢 :grin:

    3. bigfa的头像

      @chancat 硬盘肯定没有内存快,但访问量不是特别大也差不了太多 :wink:

  10. 郑永的头像

    老了,看你一路过来,这些年进步挺多,发了不少主题和技术文,继续加油。 :idea:

发表留言

人生在世,错别字在所难免,无需纠正。

:mrgreen::neutral::twisted::arrow::shock::smile::???::cool::evil::grin::idea::oops::razz::roll::wink::cry::eek::lol::mad::sad::!::?: Smilies powered by wp-alu

Post Comment
More Stories

2013 – 2014 跨年博客

December 31, 2013

2016 – 2017 跨年博客

December 31, 2016

喜提垃圾分类

October 13, 2019