老谢博客

  • 首页
  • 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 回复
发表评论 点击取消评论.

*必填

*必填

  • 文章归档
  • 子网计算
  • 我的共享
  • 锻炼计划
  • 给我留言
  • 关于老谢
2025 年 6 月
一 二 三 四 五 六 日
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« 5 月    

最新文章

  • 认知,是否是一座大山?当架构决策变成配置清单比价
  • 重装博客服务器环境
  • 特斯拉24款标续 Model Y 2万公里使用体验
  • 接盘的傻子
  • 小牛us电瓶指示灯闪三次不上电
  • 一次还不错的小米售后体验
  • 装台1600元办公主机
  • 2021好久没更新博客
  • Zabbix监控oxidized备份状态
  • Zabbix 5.0 LTS版本MySQL表分区及编译安装随记

最新评论

  • zwwooooo:类似以前做网站开发时,一开始有自...
  • 老陳网志:有点高端,像我们整点nas玩玩就够...
  • springwood:自从 CentOS 不维护之后,我换 U...
  • 大D:难都搞下来了,那就更得YM了
  • 大D:只能是YM了,谢总牛啊
  • 灰常记忆:经济不好 今年我也换了机器 一...
  • 大峰:这是海外服务器嘛?速度挺快的。
  • 大D:只能单走一个6了哈哈哈
  • zwwooooo:买特斯拉和买iPhone的人群其实相似...
  • 平安家属子痕:一直坚持油车,看你写的心里有...

日志存档

  • 2025 年 5 月
  • 2025 年 4 月
  • 2025 年 3 月
  • 2024 年 9 月
  • 2024 年 5 月
  • 2024 年 1 月
  • 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-2025 老谢博客 All rights reserved.
Gzipped 76.5% | Optimized loading 50 queries in 0.504 seconds | Memory 38.99 MB | 尼玛的备案
Powered by WordPress. | Hosted By LAOXUEHOST | Theme by WordPress主题巴士 | 站点地图 | SiteMap | uptime查询