当前位置:论坛首页 > Linux面板 > Linux面板教程

WordPress+Pastcgi_cache+Nginx_Helper

发表在 Linux面板2022-8-29 16:29 [复制链接] 2 4000

前言

Nginx版本从0.7.48开始,支持了类似Squid的缓存功能。这个缓存是把URL及相关组合当做Key,用Md5算法对Key进行哈希,得到硬盘上对应的哈希目录路径,从而将缓存内容保存在该目录内。

Nginx Web 缓存服务只能为指定URL或状态码设置过期时间,不支持类似Squid的PURGE指令手动清除缓存;但是我们可以通过Nginx的模块ngx_cache_purge清除指定URL的缓存。
fastcgi_cache缓存fastcgi生成的内容,很多情况是php生成的动态的内容,少了nginx与php的通信的次数,更减轻了php和数据库(mysql)的压力,这比用memcached之类的缓存要轻松得多

环境:
Nginx1.22
PHP7.4
WordPress 6.0.1


一、部署WordPress程序,部署过程此处省略,使用ab命令进行压测访问。
1.1 Ubuntu安装ab命令:
  1. apt-get install apache2-utils
复制代码
1.2 Centos安装ab命令:
  1. yum -y install httpd-tools
复制代码
1.3 ab命令压测
  1. ab -n 100 -c 10 http://192.168.66.172/
复制代码
1.png
-n:100次请求-c:10个并发请求压力测试
Document Length:        55668 bytes  #请求的页面大小

二、FastCGI缓存配置
1.1 编辑对应网站配置文件,添加对应参数.
  1. fastcgi_cache_key "$scheme$request_method$host$request_uri";
  2. fastcgi_cache_path /dev/shm/fastcgi-cache levels=1:2 keys_zone=WORDPRESS:100m inactive=60m;
  3. fastcgi_cache_use_stale error timeout invalid_header http_500;
  4. fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
复制代码

2.png

部分参数详解:
1.1.1 fastcgi_cache_path:
指令指定缓存(/dev/shm/fastcgi-cache)的位置,其大小(100m)。内存区域名称(WORDPRESS),子目录级别和非活动定时器。
位置可以在硬盘上的任何地方; 但是,大小必须小于您的服务器的RAM +交换,否则你会收到一个错误,“无法分配内存”。 如果缓存在“inactive”选项指定的特定时间内没有被访问(这里为60分钟),Nginx将删除它
1.1.2 fastcgi_cache_key:
令指定如何哈希缓存文件名。 Nginx基于此指令使用MD5加密访问的文件。
1.1.3 fastcgi_cache_use_stale:
定义哪些情况下用过期缓存
1.1.4 fastcgi_ignore_headers:
默认情况下fastcgi_cache会忽略有特殊header的请求,并不进行缓存,官网说明。但当我们添加这个参数后,这些限制将不在存在。

1.2 创建缓存目录,并将网站内引用的PHP配置注释。
  1. mkdir /dev/shm/fastcgi-cache  -p
复制代码
3.png
1.3 将下面参数内容添加到server字段内,如果开了SSL的话,添加到#SSL-END下面
  1. set $skip_cache 0;
  2.         if ($request_method = POST) {
  3.             set $skip_cache 1;
  4.         }   
  5.         if ($query_string != "") {
  6.             set $skip_cache 1;
  7.         }   
  8.         if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
  9.             set $skip_cache 1;
  10.         }   
  11.        if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
  12.             set $skip_cache 1;
  13.         }
  14.        location ~ [^/]\.php(/|$)
  15.             {
  16.                 try_files $uri =404;
  17.                 fastcgi_pass unix:/tmp/php-cgi-74.sock;
  18.                 fastcgi_index index.php;
  19.                 include fastcgi.conf;  
  20.                 add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
  21.                 fastcgi_cache_bypass $skip_cache;
  22.                 fastcgi_no_cache $skip_cache;
  23.                 add_header X-Cache "$upstream_cache_status From $host";
  24.                 fastcgi_cache WORDPRESS;
  25.                 add_header Cache-Control  max-age=0;
  26.                 add_header Nginx-Cache "$upstream_cache_status";
  27.                 add_header Last-Modified $date_gmt;
  28.                 add_header X-Frame-Options SAMEORIGIN;
  29.                 add_header X-Content-Type-Options nosniff;
  30.                 add_header X-XSS-Protection "1; mode=block";
  31.                 etag  on;
  32.                 fastcgi_cache_valid 200 301 302 1d;
  33.         }

  34.       location ~ /purge(/.*) {
  35.             allow 127.0.0.1;
  36.             allow xxx.xxx.xxx.xxx;    #更换服务器本机IP
  37.             deny all;
  38.             fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
  39.         }
  40.         
  41.         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)$ {
  42.         access_log off; log_not_found off; expires max;
  43.     }

  44.     location = /robots.txt { access_log off; log_not_found off; }
  45.     location ~ /\. { deny  all; access_log off; log_not_found off; }
复制代码
PS:以上参数过多,没办法一一解释,可自行查询对应参数了解。

1.4 wp的插件商店内安装 Nginx Helper插件然后配置,需要勾选Enable Purge保存启用。
4.png

PS:
1.1.1 由于插件作者定义的缓存路径是在/var/run/nginx-cache,根据自己的实际情况修改缓存路径,如果缓存路径不一致会导致插件找不到缓存文件而删除

1.1.2 找到wp程序的wp-config.php配置文件加入下面代码
  1. <div><font color="#111111" face="system-ui"><span style="font-size: 14.6667px;">define('RT_WP_NGINX_HELPER_CACHE_PATH','/dev/shm/nginx-cache');</span></font></div><div></div>
复制代码
5.png

1.5 访问测试是否有无名称缓存
6.png


1.6 在使用ab压测命令进行压测查看对比
配置Pastcgi_cache后压测:
7.png

未配置Pastcgi_cache进行压测:
8.png


使用道具 举报 只看该作者 回复
发表于 2023-3-31 17:25:45 | 显示全部楼层
在吗?咨询你个问题
使用道具 举报 回复 支持 反对
发表于 2023-4-13 16:55:23 | 显示全部楼层
在吗,大哥
求助
使用道具 举报 回复 支持 反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

企业版年付运维跟进群

普通问题处理

论坛响应时间:72小时

问题处理方式:排队(仅解答)

工作时间:白班:9:00 - 18:00

紧急问题处理

论坛响应时间:10分钟

问题处理方式:1对1处理(优先)

工作时间:白班:9:00 - 18:00

工作时间:晚班:18:00 - 24:00

立即付费处理
快速回复 返回顶部 返回列表