云如梭 發表於 2023-12-14 00:00:00

WordPress速度优化-Nginx fastcgi_cache缓存加速

<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
高并发网站架构的核心原则其实就一句话“把所有的用户访问请求都尽量往前推“,即:能缓存在用户电脑本地的,就不要让他去访问CDN。 能缓存CDN服务器上的,就不要让CDN去访问源(静态服务器)了。能访问静态服务器的,就不要去访问动态服务器。以此类推:能不访问数据库和存储就一定不要去访问数据库和存储。 <br><br>
WordPress最好的优化方式就是尽量不安装插件,Wordpress是典型的PHP-MySQL应用,去做数据库缓存,倒不如让轻量级的Nginx直接去缓存WordPress内容。 <br><br>
Nginx内置FastCgi缓存,但是不支持自动清除缓存。当你在Wordpress里面新建/修改一篇文章,或者访客提交评论的时候,自动清空相关的缓存是必要的!Nginx需要安装ngx_cache_purg+量身定做的WordPress缓存清理插件:Nginx Helper。 <br><br><strong>1. 安装Nginx ngx_cache_purge模块</strong> <br>
1)查看ngx_cache_purge是否安装 <br><br>
nginx -V 2&gt;&amp;1 | grep -o ngx_cache_purge <br><br>
显示ngx_cache_purge表示已经安装 <br><br>
2)安装ngx_cache_purge模块 <br><br>
《OneinStack》和《lnmp一键安装包》下安装ngx_cache_purge模块 <br><br>
 </p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
代码如下:</div>
<div id="phpcode5" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
cd /root/oneinstack/src<br>
wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz<br>
wget http://nginx.org/download/nginx-1.9.10.tar.gz<br>
tar xzf ngx_cache_purge-2.3.tar.gz<br>
tar xzf nginx-1.9.10.tar.gz<br>
cd nginx-1.9.10<br>
nginx -V #查看nginx编译参数,最后加上--add-module=../ngx_cache_purge-2.3<br>
./configure --prefix=/usr/local/nginx --user=www --group=www \<br>
--with-http_stub_status_module --with-http_v2_module --with-http_ssl_module \<br>
--with-ipv6 --with-http_gzip_static_module --with-http_realip_module \<br>
--with-http_flv_module --with-ld-opt=-ljemalloc \<br>
--add-module=../ngx_cache_purge-2.3<br>
make #编译<br>
mv /usr/local/nginx/sbin/nginx{,_`date +%F`} #备份nginx<br>
cp objs/nginx /usr/local/nginx/sbin<br>
nginx -V 2&gt;&amp;1 | grep -o ngx_cache_purge<br>
# 显示ngx_cache_purge表示已经安装成功</div>
<br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><strong style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>2. Nginx配置</strong><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>建议将fastcgi_cache_path设置tmpfs内存中,操作系统不同tmpfs路径也不同,如下:</span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'> </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'> CentOS:/dev/shm</span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>Ubuntu和Debian:/run/shm</span>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
 </p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
修改nginx虚拟主机配置文件/usr/local/nginx/conf/vhost/blog.linuxeye.com.conf:<br><br>
 </p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
代码如下:</div>
<div id="phpcode6" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
fastcgi_cache_path /dev/shm/nginx-cache levels=1:2 keys_zone=WORDPRESS:100m inactive=60m;<br>
fastcgi_cache_key "$scheme$request_method$host$request_uri";<br>
fastcgi_cache_use_stale error timeout invalid_header http_500;<br>
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;<br>
server {<br>
listen 443 ssl http2;<br>
ssl_certificate /usr/local/nginx/conf/vhost/linuxeye_blog.crt;<br>
ssl_certificate_key /usr/local/nginx/conf/vhost/linuxeye_blog.key;<br>
ssl_ciphers "CHACHA20:GCM:HIGH:!DH:!RC4:!aNULL:!eNULL:!LOW:!3DES:!MD5:!EXP:!PSK:!SRP:!DSS";<br>
ssl_session_cache shared:SSL:10m;<br>
ssl_session_timeout 10m;<br>
#ssl_stapling on;<br>
#ssl_stapling_verify on;<br>
resolver 8.8.8.8 8.8.4.4 valid=300s;<br>
resolver_timeout 5s;<br>
server_name blog.linuxeye.com;<br>
access_log /home/wwwlogs/blog_nginx.log combined;<br>
index index.html index.htm index.php;<br>
include wordpress.conf;<br>
root /home/wwwroot/blog;<br>
set $skip_cache 0;<br>
if ($request_method = POST) {<br>
set $skip_cache 1;<br>
}<br>
if ($query_string != "") {<br>
set $skip_cache 1;<br>
}<br>
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {<br>
set $skip_cache 1;<br>
}<br>
if ($http_cookie ~* "comment_author|wordpress_+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {<br>
set $skip_cache 1;<br>
}<br>
location ~ [^/]\.php(/|$) {<br>
#fastcgi_pass remote_php_ip:9000;<br>
fastcgi_pass unix:/dev/shm/php-cgi.sock;<br>
fastcgi_index index.php;<br>
include fastcgi.conf;<br>
fastcgi_cache_bypass $skip_cache;<br>
fastcgi_no_cache $skip_cache;<br>
fastcgi_cache WORDPRESS;<br>
fastcgi_cache_valid 60m;<br>
}<br>
location ~ /purge(/.*) {<br>
allow 127.0.0.1;<br>
deny all;<br>
fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";<br>
}<br>
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {<br>
expires 30d;<br>
access_log off;<br>
}<br>
location ~ .*\.(js|css)?$ {<br>
expires 7d;<br>
access_log off;<br>
}<br>
}</div>
<br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>使nginx配置生效</span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'> </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
代码如下:</div>
<div id="phpcode7" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
service nginx reload</div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
 </p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
重启系统后shm内存中nginx-cache文件夹会丢失,为了使重启生效(自动创建文件夹),修改/etc/init.d/nginx的make_dirs下一行添加:<br>
 </p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
代码如下:</div>
<div id="phpcode8" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
[ ! -d '/dev/shm/nginx-cache' ] &amp;&amp; { mkdir /dev/shm/nginx-cache; chown -R ${user}.$user /dev/shm/nginx-cache; }</div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
 </p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
<strong>3. WordPress安装Nginx Helper插件</strong><br>
WordPress后台【插件】—【安装插件】搜索【Nginx Helper】安装即可。如下设置:<br><br><img title="WordPress速度优化-Nginx fastcgi_cache缓存加速" alt="WordPress速度优化-Nginx fastcgi_cache缓存加速" src="https://zhuji.jb51.net/uploads/img/202305/0635ba34ea99b6af65cf664c83cb08fa.jpg" style="max-width:100%!important;height:auto!important;border: 1px solid rgb(204, 204, 204); vertical-align: middle; padding: 1px; overflow: hidden; max-width: 696px; width: 677px; height: 439px;"><br><br>
修改wordpress网站根目录wp-config.php添加如下行: <br>
 </p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
代码如下:</div>
<div id="phpcode9" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
define('RT_WP_NGINX_HELPER_CACHE_PATH','/dev/shm/nginx-cache'); </div>
<br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><strong style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>4. 测试 </strong><br><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>测试时候勾选Enable Logging(记录日志)、Enable Nginx Timestamp in HTML(插入缓存信息) </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>查看Nginx Helper是否刷新日志: </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><img title="WordPress速度优化-Nginx fastcgi_cache缓存加速" alt="WordPress速度优化-Nginx fastcgi_cache缓存加速" src="https://zhuji.jb51.net/uploads/img/202305/9465948e1e9a71cd605b1c9f58b232d1.jpg" style="max-width:100%!important;height:auto!important;border: 1px solid rgb(204, 204, 204); vertical-align: middle; padding: 1px; overflow: hidden; max-width: 696px; font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px; width: 450px; height: 300px;'>
頁: [1]
查看完整版本: WordPress速度优化-Nginx fastcgi_cache缓存加速