老谢博客

  • 首页
  • WordPress
  • 网络技术
  • 乱七八糟
  • 运维技术
  • 给我留言
  • 关于老谢

Nginx配置fastcgi_cache缓存PHP输出为WordPress加速

分类:网站技术日期:2019-07-15 - 0:54:52作者:老谢

  闲来无事折腾一波,好久没折腾Nginx了。

  高并发网站架构的核心原则其实就一句话”把所有的用户访问请求都尽量往前推“,即:能缓存在用户电脑本地的,就不要让他去访问 CDN。 能缓存 CDN 服务器上的,就不要让 CDN 去访问源(静态服务器)了。能访问静态服务器的,就不要去访问动态服务器。以此类推:能不访问数据库和存储就一定不要去访问数据库和存储。

安装ngx_cache_purge模块

  ngx_cache_purge是为了后面配合插件完成自动刷新使用,ngx_cache_purge是个控制Nginx缓存的模块。

  军哥LNMP一键包默认不编译ngx_cache_purge模块,所以需要自行编译该模块进去,使用nginx -V命令可以看到当前的编译参数,编译该模块只需要增添–-add-module=../ngx_cache_purge-2.3参数即可,顺便升级一波Nginx。

wget http://nginx.org/download/nginx-1.17.0.tar.gz
wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
tar -zxf nginx-1.17.0.tar.gz
tar -zxf ngx_cache_purge-2.3.tar.gz
cd nginx-1.17.0
./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.0.2o --with-ld-opt=-Wl,-rpath,/usr/local/luajit/lib --add-module=/root/lnmp1.5/src/lua-nginx-module-0.10.11 --add-module=/root/lnmp1.5/src/ngx_devel_kit-0.3.0 --add-module=/root/ngx_cache_purge-2.3
make
make upgrade
/etc/init.d/nginx stop
cp objs/nginx /usr/local/nginx/sbin/nginx
/etc/init.d/nginx start

wget http://nginx.org/download/nginx-1.17.0.tar.gz wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz tar -zxf nginx-1.17.0.tar.gz tar -zxf ngx_cache_purge-2.3.tar.gz cd nginx-1.17.0 ./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.0.2o --with-ld-opt=-Wl,-rpath,/usr/local/luajit/lib --add-module=/root/lnmp1.5/src/lua-nginx-module-0.10.11 --add-module=/root/lnmp1.5/src/ngx_devel_kit-0.3.0 --add-module=/root/ngx_cache_purge-2.3 make make upgrade /etc/init.d/nginx stop cp objs/nginx /usr/local/nginx/sbin/nginx /etc/init.d/nginx start

  升级后nginx -t报错,新版Nginx已经不需要ssl on;,直接在监听的端口后加ssl参数即可,下面检查ngx_cache_purge有没有编译成功。

[root@laoxie~]# nginx -V 2>&1 | grep -o ngx_cache_purge
ngx_cache_purge
 
[root@laoxie ~]# nginx -V
nginx version: nginx/1.17.0
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-23) (GCC)
built with OpenSSL 1.0.2o  27 Mar 2018
TLS SNI support enabled
configure arguments: --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.0.2o --with-ld-opt=-Wl,-rpath,/usr/local/luajit/lib --add-module=/root/lnmp1.5/src/lua-nginx-module-0.10.11 --add-module=/root/lnmp1.5/src/ngx_devel_kit-0.3.0 --add-module=/root/ngx_cache_purge-2.3

[root@laoxie~]# nginx -V 2>&1 | grep -o ngx_cache_purge ngx_cache_purge [root@laoxie ~]# nginx -V nginx version: nginx/1.17.0 built by gcc 4.4.7 20120313 (Red Hat 4.4.7-23) (GCC) built with OpenSSL 1.0.2o 27 Mar 2018 TLS SNI support enabled configure arguments: --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.0.2o --with-ld-opt=-Wl,-rpath,/usr/local/luajit/lib --add-module=/root/lnmp1.5/src/lua-nginx-module-0.10.11 --add-module=/root/lnmp1.5/src/ngx_devel_kit-0.3.0 --add-module=/root/ngx_cache_purge-2.3

增添Nginx缓存相应配置

  nginx.conf增添配置:

#路径需要提前创建好
fastcgi_cache_path /tmp/nginx_cache levels=1:2 keys_zone=WORDPRESS:250m inactive=1d max_size=500m;
fastcgi_temp_path /tmp/nginx_cache/temp;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;

#路径需要提前创建好 fastcgi_cache_path /tmp/nginx_cache levels=1:2 keys_zone=WORDPRESS:250m inactive=1d max_size=500m; fastcgi_temp_path /tmp/nginx_cache/temp; fastcgi_cache_key "$scheme$request_method$host$request_uri"; fastcgi_cache_use_stale error timeout invalid_header http_500;

  server字段增添配置:

		#ngx_cache_purge
		#post访问不缓存
		set $skip_cache 0; 
		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*") {
			 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;
				}

#ngx_cache_purge #post访问不缓存 set $skip_cache 0; 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*") { 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$ { 字段增添配置:

			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;
			# 只允许本站用 frame 来嵌套
			add_header X-Frame-Options SAMEORIGIN; 
			# 禁止嗅探文件类型
			add_header X-Content-Type-Options nosniff;
			# XSS 保护
			add_header X-XSS-Protection "1; mode=block";
			etag  on;
			fastcgi_cache WORDPRESS;
			fastcgi_cache_valid 200 301 302 1d;
 
			#忽略一切nocache申明,避免不缓存伪静态等
			fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
		        fastcgi_cache_bypass $skip_cache;
		        fastcgi_no_cache $skip_cache;

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; # 只允许本站用 frame 来嵌套 add_header X-Frame-Options SAMEORIGIN; # 禁止嗅探文件类型 add_header X-Content-Type-Options nosniff; # XSS 保护 add_header X-XSS-Protection "1; mode=block"; etag on; fastcgi_cache WORDPRESS; fastcgi_cache_valid 200 301 302 1d; #忽略一切nocache申明,避免不缓存伪静态等 fastcgi_ignore_headers Cache-Control Expires Set-Cookie; fastcgi_cache_bypass $skip_cache; fastcgi_no_cache $skip_cache;

  新增字段:

#缓存清理配置(可选)
		location ~ /purge(/.*) {
			allow 127.0.0.1;
			#此处填写你的服务器IP
			allow 114.215.187.51;
			deny all;
			请注意此处的WORDPRESS要与上面的keys_zone保持一致
			fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
		}

#缓存清理配置(可选) location ~ /purge(/.*) { allow 127.0.0.1; #此处填写你的服务器IP allow 114.215.187.51; deny all; 请注意此处的WORDPRESS要与上面的keys_zone保持一致 fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1"; }

  在fastcgi_cache_path和fastcgi_temp_path中,有人会建议将它设置为内存路径,例如:/dev/shm/nginx-cache levels=1:2 keys_zone=WORDPRESS:100m inactive=60m;,如果你的磁盘IO很慢的话建议采用此方式,毕竟内存的读写速度非常快。

  add_header Cache-Control 如果是动态内容要实时更新的话,可以设置为0,否则可以设置时间大一些

  

访问逻辑

Nginx配置fastcgi_cache缓存PHP输出为WordPress加速

  

验证检查
[root@laoxie vhost]# curl -X GET -I https://www.xj123.info/7558.html
 
HTTP/1.1 200 OK
Server: nginx
Date: Sun, 14 Jul 2019 15:56:25 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
X-Powered-By: PHP/5.6.20
Set-Cookie: PHPSESSID=9jeedb5jkv1mb5snnccghjgd33; path=/
X-Pingback: https://www.xj123.info/xmlrpc.php
Link: <https://www.xj123.info/wp-json/>; rel="https://api.w.org/"
Link: <https://www.xj123.info/?p=7558>; rel=shortlink
Strict-Transport-Security: max-age=63072000; includeSubdomains; preload
X-Cache: MISS From www.xj123.info
Cache-Control: max-age=0
Nginx-Cache: MISS
Last-Modified: Sunday, 14-Jul-2019 15:56:25 GMT
X-Frame-Options: SAMEORIGIN
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
 
[root@laoxie vhost]# curl -X GET -I https://www.xj123.info/7558.html
 
HTTP/1.1 200 OK
Server: nginx
Date: Sun, 14 Jul 2019 15:56:32 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
X-Powered-By: PHP/5.6.20
Set-Cookie: PHPSESSID=9jeedb5jkv1mb5snnccghjgd33; path=/
X-Pingback: https://www.xj123.info/xmlrpc.php
Link: <https://www.xj123.info/wp-json/>; rel="https://api.w.org/"
Link: <https://www.xj123.info/?p=7558>; rel=shortlink
Strict-Transport-Security: max-age=63072000; includeSubdomains; preload
X-Cache: HIT From www.xj123.info
Cache-Control: max-age=0
Nginx-Cache: HIT
Last-Modified: Sunday, 14-Jul-2019 15:56:32 GMT
X-Frame-Options: SAMEORIGIN
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block

[root@laoxie vhost]# curl -X GET -I https://www.xj123.info/7558.html HTTP/1.1 200 OK Server: nginx Date: Sun, 14 Jul 2019 15:56:25 GMT Content-Type: text/html; charset=UTF-8 Transfer-Encoding: chunked Connection: keep-alive Vary: Accept-Encoding X-Powered-By: PHP/5.6.20 Set-Cookie: PHPSESSID=9jeedb5jkv1mb5snnccghjgd33; path=/ X-Pingback: https://www.xj123.info/xmlrpc.php Link: <https://www.xj123.info/wp-json/>; rel="https://api.w.org/" Link: <https://www.xj123.info/?p=7558>; rel=shortlink Strict-Transport-Security: max-age=63072000; includeSubdomains; preload X-Cache: MISS From www.xj123.info Cache-Control: max-age=0 Nginx-Cache: MISS Last-Modified: Sunday, 14-Jul-2019 15:56:25 GMT X-Frame-Options: SAMEORIGIN X-Content-Type-Options: nosniff X-XSS-Protection: 1; mode=block [root@laoxie vhost]# curl -X GET -I https://www.xj123.info/7558.html HTTP/1.1 200 OK Server: nginx Date: Sun, 14 Jul 2019 15:56:32 GMT Content-Type: text/html; charset=UTF-8 Transfer-Encoding: chunked Connection: keep-alive Vary: Accept-Encoding X-Powered-By: PHP/5.6.20 Set-Cookie: PHPSESSID=9jeedb5jkv1mb5snnccghjgd33; path=/ X-Pingback: https://www.xj123.info/xmlrpc.php Link: <https://www.xj123.info/wp-json/>; rel="https://api.w.org/" Link: <https://www.xj123.info/?p=7558>; rel=shortlink Strict-Transport-Security: max-age=63072000; includeSubdomains; preload X-Cache: HIT From www.xj123.info Cache-Control: max-age=0 Nginx-Cache: HIT Last-Modified: Sunday, 14-Jul-2019 15:56:32 GMT X-Frame-Options: SAMEORIGIN X-Content-Type-Options: nosniff X-XSS-Protection: 1; mode=block

  注意x-cache的参数,若第一次访问页面,则x-cache为MISS响应,如果是HIT则表示已经缓存,BYPASS则表示非缓存内容,上面的输出x-cache状态即为HIT,奇怪的是在Nginx中已经忽略掉了Cache-Control的输出,但HTTP头仍有cache-control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0的输出,经研究PHP.ini的session.cache_limiter参数,默认值是nocache,我们需要将它设置为none,重启php-fpm并清空Nginx缓存即可,注意一定要清空缓存目录,然后reload下nginx!!!
  
  按照Nginx中的配置,http_cookie不缓存,即若用户评论产生http_cookie即x-cache为BYPASS状态,经过测试也是没问题的。

WordPress安装Nginx Settings插件

  为了让Blog每次刷新自动清空缓存,需要wp安装Nginx Settings插件来配合,安装就直接在wp后台搜索插件在线安装即可,安装完启动,进入插件设置选项,勾选Enable Purge保存即可,但是插件作者定义的缓存目录是/var/run/nginx-cache,我实际的缓存目录是/tmp/nginx_cache,由于缓存路径不同,刷新肯定会有问题,解决方法是在wp-config.php定义缓存目录:

//根据实际情况定义缓存的存放路径
define( 'RT_WP_NGINX_HELPER_CACHE_PATH','/tmp/wpcache');

//根据实际情况定义缓存的存放路径 define( 'RT_WP_NGINX_HELPER_CACHE_PATH','/tmp/wpcache');

  系统中建立软连接:

ln -s /tmp/nginx_cache/ /tmp/wpcache

ln -s /tmp/nginx_cache/ /tmp/wpcache

  配置完成后,可以更新一篇帖子,然后再刷新HIT的页面,看看x-cache状态是否改变为Miss,若为Miss则表示之前缓存已刷新!

总结

  虽然折腾的不是很费劲,但很多参数一知半解,还是得抽个空学习下HTTP协议才能玩好Nginx。

参考文章:

用Nginx fastcgi_cache缓存为你的PHP网站加速
Nginx的fastcgi_cache缓存加速,ngx_cache_purg清理缓存,WordPress的Nginx Helper
Nginx模块fastcgi_cache的几个注意点
WordPress开启Nginx fastcgi_cache缓存加速方法-Nginx配置实例
无缝升级为nginx添加ngx_cache_purge模块

原文地址 : https://www.xj123.info/7650.html

本站遵循 : 署名-非商业性使用-相同方式共享 2.5 中国大陆 (CC BY-NC-SA 2.5)

版权声明 : 原创文章转载时,请务必以超链接形式标明文章原始出处

Tags: nginx , WordPress
  • 上一篇:群晖DS216 Play迁移旧硬盘至新群晖DS718+
  • 下一篇:php 7.3.7安装Mcrypt扩展
5条评论
  1. Jason 说:

    测试cookie

    POST:2019-07-15 00:22 回复
  2. 奶爸建网站笔记 说:

    我折腾了这个的,感觉和m开头的效果差不多。

    POST:2019-07-15 11:45 回复
    • 老谢 说:

      memcached毕竟还是动态获取,这是纯静态缓存

      POST:2019-07-15 15:24 回复
  3. 心灵博客 说:

    就是清理缓存比较麻烦,还好wp的插件能搞定,其他程序弄起来就比较麻烦了。

    POST:2019-07-15 22:21 回复
    • 老谢 说:

      wp这个插件是很方便

      POST:2019-07-17 23:21 回复
发表评论 点击取消评论.

*必填

*必填

  • 文章归档
  • 子网计算
  • 我的共享
  • 锻炼计划
  • 给我留言
  • 关于老谢
2023年 5月
一 二 三 四 五 六 日
1234567
891011121314
15161718192021
22232425262728
293031  
« 4月    

最新文章

  • 装台1600元办公主机
  • 2021好久没更新博客
  • Zabbix监控oxidized备份状态
  • Zabbix 5.0 LTS版本MySQL表分区及编译安装随记
  • centos7.9部署oxidized自动备份交换机配置
  • Surface Pro 4更换屏幕与电池
  • VCSA中删除horizon view链接克隆生成的replica-受保护副本
  • Esxi6.7U3安装SanDisk Fusion-io 1.3T ioscale Pci SSD加速卡驱动
  • 搭建ELK日志系统分析处理fortigate的syslog日志
  • 华为USG防火墙配置NAT映射回流解决内网通过公网映射访问内部服务器

最新评论

  • 王光卫博客:以前也喜欢DIY现在玩不动了
  • 大D:大佬!
  • Sirit:够用就好,不过现在的电子产品是真的...
  • 美樂地:我也想买个mini PC,打算等下一代的...
  • Mr.Chou:老牌子又不用带独显够用的,如换是...
  • 小熊:评论区全是大佬,就我完全不太懂。
  • 老俍:真是巧了,我也想给苏菲4pro换电池呢,...
  • 空空裤兜:all in one的小主机都有两千的了。...
  • Pampo:1600组一个12100的不错了,价格压挺低...
  • 无敌风火轮:和索尼没啥关系,这是索尼电脑的...

日志存档

  • 2023年4月
  • 2021年10月
  • 2021年4月
  • 2021年3月
  • 2021年2月
  • 2020年11月
  • 2020年9月
  • 2020年5月
  • 2020年4月
  • 2020年3月
  • 2020年1月
  • 2019年12月
  • 2019年10月
  • 2019年7月
  • 2019年6月
  • 2019年5月
  • 2019年3月
  • 2019年1月
  • 2018年12月
  • 2018年11月
  • 2018年10月
  • 2018年7月
  • 2018年6月
  • 2018年5月
  • 2018年4月
  • 2018年3月
  • 2018年1月
  • 2017年10月
  • 2017年9月
  • 2017年8月
  • 2017年7月
  • 2017年2月
  • 2017年1月
  • 2016年12月
  • 2016年11月
  • 2016年10月
  • 2016年7月
  • 2016年6月
  • 2016年4月
  • 2016年2月
  • 2016年1月
  • 2015年12月
  • 2015年10月
  • 2015年9月
  • 2015年7月
  • 2015年5月
  • 2015年4月
  • 2015年3月
  • 2015年2月
  • 2015年1月
  • 2014年12月
  • 2014年10月
  • 2014年9月
  • 2014年8月
  • 2014年7月
  • 2014年6月
  • 2014年5月
  • 2014年4月
  • 2014年3月
  • 2014年2月
  • 2014年1月
  • 2013年12月
  • 2013年11月
  • 2013年10月
  • 2013年9月
  • 2013年8月
  • 2013年7月
  • 2013年6月
  • 2013年5月
  • 2013年4月
  • 2013年3月
  • 2013年2月
  • 2013年1月
  • 2012年12月
  • 2012年11月
  • 2012年9月
  • 2012年8月
  • 2012年7月
  • 2012年6月
  • 2012年5月
  • 2012年4月
  • 2012年3月
  • 2012年2月
  • 2012年1月
  • 2011年12月
  • 2011年11月
  • 2011年10月
  • 2011年9月
  • 2011年8月
  • 2011年7月
  • 2011年6月
  • 2011年5月
  • 2011年4月
  • 2011年3月
  • 2011年2月
  • 2011年1月
  • 2010年12月
  • 2010年11月
  • 2010年10月
  • 2010年9月
  • 2010年8月
  • 2010年7月

W3C

  • XHTML 1.0 Transitional
  • CSS level 3
  • Google+
Copyright © 2010-2023 老谢博客 All rights reserved.
Gzipped 76.5% | Optimized loading 2 queries in 0.243 seconds | Memory 36.65 MB | 皖ICP备13010663号-1
Powered by WordPress. | Hosted By 腾讯云 | Theme by WordPress主题巴士 | 站点地图 | SiteMap | Uptime | 技术支持:苏州天剑计算机系统有限公司