wordpress宝塔面板开启Nginx fastcgi_cache缓存
说到wordpress的缓存,大家想到的肯定是WP-Super-Cache的静态html缓存,以及redis动态缓存,插件的缓存效果肯定是有的,但是容易出现各种问题,比如配置很复杂、配置全英文、插件之间的冲突等,所以今天新酱要教大家一个更高级的缓存:Nginx fastcgi_cache缓存,直接在nginx层面缓存页面,还支持缓存伪静态!效果比起传统的php缓存好得太多了,因为很多人使用宝塔面板,所以今天的教程是基于宝塔面板的教程。
查询是否安装Nginx ngx_cache_purge模块
用xshell等远程连接工具连接你的服务器或vps,输入下面的命令
1 |
nginx -V 2>&1 | grep -o ngx_cache_purge |
出现ngx_cache_purge则代表已经安装,现在宝塔默认安装ngx_cache_purge模块,所以一般不需要手动编译安装。
Nginx配置
现在我们开始配置nginx,来到宝塔后台,找到你要配置的网站,点击配置文件,将下面的代码贴到server的上面。
1 2 3 4 5 6 |
fastcgi_cache_path /tmp/wpcache levels=1:2 keys_zone=WORDPRESS:50m inactive=1d max_size=1G; fastcgi_temp_path /tmp/wpcache/temp; fastcgi_cache_key "$scheme$request_method$host$request_uri"; fastcgi_cache_use_stale error timeout invalid_header http_500; #忽略一切nocache申明,避免不缓存伪静态等 fastcgi_ignore_headers Cache-Control Expires Set-Cookie; |
/tmp/wpcache和/tmp/wpcache/temp文件要自己创建,否则可能会出现nginx无法启动的情况,
然后将下面的代码贴到#SSL-END下面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
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; } #这里请参考你网站之前的配置,特别是sock的路径,弄错了就502了!关于sock,宝塔面板的在/www/server/php/你的php版本/etc/php-fpm.conf中有配置路径宝塔面板的sock location ~ [^/]\.php(/|$) { try_files $uri =404; fastcgi_pass unix:/tmp/php-cgi-71.sock; fastcgi_index index.php; include fastcgi.conf; 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"; fastcgi_cache WORDPRESS; add_header Cache-Control max-age=0; add_header Nginx-Cache "$upstream_cache_status"; add_header Last-Modified $date_gmt; add_header X-Frame-Options SAMEORIGIN; # 只允许本站用 frame 来嵌套 add_header X-Content-Type-Options nosniff; # 禁止嗅探文件类型 add_header X-XSS-Protection "1; mode=block"; # XSS 保护 etag on; fastcgi_cache_valid 200 301 302 1d; } #缓存清理配置(可选模块,请细看下文说明) location ~ /purge(/.*) { allow 127.0.0.1; allow "你的外网ip";# 引号要保留 deny all; fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1"; } |
完整的配置例子如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
fastcgi_cache_path /tmp/wpcache levels=1:2 keys_zone=WORDPRESS:50m inactive=1d max_size=1G; fastcgi_temp_path /tmp/wpcache/temp; fastcgi_cache_key "$scheme$request_method$host$request_uri"; fastcgi_cache_use_stale error timeout invalid_header http_500; #忽略一切nocache申明,避免不缓存伪静态等 fastcgi_ignore_headers Cache-Control Expires Set-Cookie; server { listen 80; listen 443 ssl http2; server_name www.dongmanzhijia.top; index index.php index.html index.htm default.php default.htm default.html; root /www/wwwroot/www.dongmanzhijia.top; #SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则 #error_page 404/404.html; #HTTP_TO_HTTPS_START if ($server_port !~ 443){ rewrite ^(/.*)$ https://$host$1 permanent; } #HTTP_TO_HTTPS_END ssl_certificate /etc/letsencrypt/live/www.dongmanzhijia.top/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/www.dongmanzhijia.top/privkey.pem; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; ssl_prefer_server_ciphers on; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; error_page 497 https://$host$request_uri; #SSL-END 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; } #这里请参考你网站之前的配置,特别是sock的路径,弄错了就502了!关于sock,宝塔面板的在/www/server/php/你的php版本/etc/php-fpm.conf中有配置路径宝塔面板的sock location ~ [^/]\.php(/|$) { try_files $uri =404; fastcgi_pass unix:/tmp/php-cgi-71.sock; fastcgi_index index.php; include fastcgi.conf; 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"; fastcgi_cache WORDPRESS; add_header Cache-Control max-age=0; add_header Nginx-Cache "$upstream_cache_status"; add_header Last-Modified $date_gmt; add_header X-Frame-Options SAMEORIGIN; # 只允许本站用 frame 来嵌套 add_header X-Content-Type-Options nosniff; # 禁止嗅探文件类型 add_header X-XSS-Protection "1; mode=block"; # XSS 保护 etag on; fastcgi_cache_valid 200 301 302 1d; } #缓存清理配置(可选模块,请细看下文说明) location ~ /purge(/.*) { allow 127.0.0.1; allow "你的外网ip"; deny all; fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1"; } #ERROR-PAGE-START 错误页配置,可以注释、删除或修改 error_page 404 /404.html; error_page 502 /502.html; #ERROR-PAGE-END #PHP-INFO-START PHP引用配置,可以注释或修改 include enable-php-71.conf; #PHP-INFO-END #REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效 include /www/server/panel/vhost/rewrite/www.dongmanzhijia.top.conf; #REWRITE-END #禁止访问的文件或目录 location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md) { return 404; } #一键申请SSL证书验证目录相关设置 location ~ \.well-known{ allow all; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|webp)$ { expires 30d; error_log off; access_log off; } location ~ .*\.(js|css)?$ { expires 12h; error_log off; access_log off; } location ~ .*\.(eot|ttf|otf|woff|svg)?$ { expires max; error_log off; access_log off; } access_log /www/wwwlogs/www.dongmanzhijia.top.log; error_log /www/wwwlogs/www.dongmanzhijia.top.error.log; } |
代码最开头这里需要注意一下
1 |
fastcgi_cache_path /tmp/wpcache levels=1:2 keys_zone=WORDPRESS:50m inactive=1d max_size=1G; |
WORDPRESS:50m的意思是分给缓存50M的内存,如果你的可用内存少于50M会出现保存失败,同理,你的内存很大,你可以分配500M都没问题。这个按配置来,我的内存只有500M,所以我分配了50M。
安装Nginx Helper插件
后台搜索Nginx Helper插件安装启用,这个插件是为wordpress fastcgi_cache缓存打造的一个插件,十分的好用。
插件设置页面如下:
这是新酱博客的缓存设置选项(2021/1/31文字更新,清理缓存如果不用cdn,直接使用第一个PURGE清理,因为第二个发表评论的时候会因为open_basedir的原因导致清理失败!)
各种选项的含义可以查看下图
最后还有两个选项,按需开启即可
配置好之后保存。
保存后还需要修改插件默认的缓存目录,找到插件目录下的nginx-helper\includes\class-nginx-helper.php,修改里面的/var/run/nginx-cache为/tmp/wpcache,不然插件无法清理缓存!
缓存效果
保存重启nginx后就可以看到缓存效果啦,如下图所示:
首次访问会显示BYPASS,第二次就缓存了。
评论