宝塔面板配置 Nginx Cache 缓存教程

系统环境:ubuntu 22.04
宝塔面板:7.9.8
Nginx :1.18
PHP :7.4
Mysql :8.0

注:默认宝塔安装的 nginx 已经编译了 nginx cache 和 nginx purge 相关模块,无需额外编译安装模块。

添加下方配置到 nginx 配置文件的 http {…} 段落中,

  1. #http {...}
  2. fastcgi_cache_path /www/wwwroot/www.yourname.com/wp-content/cache/nginx-cache levels=1:2 keys_zone=yourname:400m max_size=4g inactive=72h use_temp_path=off;#此处设置缓存名
  3. fastcgi_cache_key "$scheme$request_method$host$request_uri";

fastcgi_cache_path /www/…/nginx-cache 缓存路径,可以随便设定。
levelslevels=1:2 缓存目录层级,保持默认即可。
keys_zone=yourname:400m 缓存区域名 yourname,后面的 :400m 是缓存键值可用内存空间。
max_size=4g 缓存最大占用磁盘空间。
inactive=72h 缓存过期时间。
use_temp_path=off 禁用缓存临时存储,直接写入到指定位置,避免多磁盘分区跨区写入造成IO消耗。

如果当前主机有其他站点需要 nginx 缓存,那么第二个站点只需添加上方配置到 http {…} 段落中,然后修改第一行 fastcgi_cache_path、keys_zone 这两项的值即可,最后并把第二行 fastcgi_cache_key 这行注释掉。

参考案例:

  1. #添加到网站配置文件顶部
  2. #http {...}
  3. fastcgi_cache_path /www/wwwroot/www.yourname.com/wp-content/cache/nginx-cache levels=1:2 keys_zone=yourname:400m max_size=4g inactive=72h use_temp_path=off;#此处设置缓存名
  4. fastcgi_cache_key "$scheme$request_method$host$request_uri";
  5. server
  6. {
  7. listen 80 reuseport;
  8. listen 443 ssl http2 reuseport;
  9. listen [::]:443 ssl http2 reuseport;
  10. listen [::]:80 reuseport;
  11. server_name www.yourname.com yourname.com;
  12. index index.php index.html index.htm default.php default.htm default.html;
  13. root /wwww/wwwroot/www.yourname.com;
  14. #SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
  15. #error_page 404/404.html;
  16. ssl_certificate /www/server/panel/vhost/cert/www.yourname.com/fullchain.pem;
  17. ssl_certificate_key /www/server/panel/vhost/cert/www.yourname.com/privkey.pem;
  18. ssl_early_data on;
  19. ssl_protocols TLSv1.2 TLSv1.3;
  20. ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
  21. ssl_prefer_server_ciphers on;
  22. ssl_session_cache shared:SSL:10m;
  23. ssl_session_timeout 10m;
  24. add_header Strict-Transport-Security "max-age=31536000";
  25. error_page 497 https://$host$request_uri;
  26. #SSL-END
  27. #引用重定向规则,注释后配置的重定向代理将无效
  28. include /www/server/panel/vhost/nginx/redirect/www.yourname.com/*.conf;
  29. #ERROR-PAGE-START 错误页配置,可以注释、删除或修改
  30. #error_page 404 /404.html;
  31. #error_page 502 /502.html;
  32. #ERROR-PAGE-END
  33. #PHP-INFO-START PHP引用配置,可以注释或修改
  34. #include enable-php-74.conf;#重点!注销宝塔默认配置!
  35. #PHP-INFO-END
  36. #REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效
  37. include /www/server/panel/vhost/rewrite/www.yourname.com.conf;
  38. #REWRITE-END
  39. ************* 下方省略 **************

将下面的内容添加到宝塔网站设置的伪静态规则中,需修改下方规则中的 yourname 对应上面你设定的 keys_zone 值。

  1. #server {...}
  2. set $skip_cache 0;
  3. #如果请求方式为 POST 则绕过缓存
  4. if ($request_method = POST) {
  5. set $skip_cache 1;
  6. }
  7. if ($query_string != "") {
  8. set $skip_cache 1;
  9. }
  10. #如果请求链接含下方内容则绕过缓存
  11. if ($request_uri ~* "/wp-admin/|/denglu/|/checkout/|/my-account/|/cart/|/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.(xml|xsl)|[a-z0-9_-]+-sitemap([0-9]+)?.(xml|xsl)") {
  12. set $skip_cache 1;
  13. }
  14. #如果携带 cookie 包含下方内容则跳过
  15. if ($http_cookie ~* "comment_author|comment_|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpressuser_|wordpresspass_|wordpress_sec_|wordpress_logged_in_|woocommerce_items_in_cart|yith_wcwl_products") {
  16. set $skip_cache 1;
  17. }
  18. #添加可以绕过缓存的终端IP地址
  19. #if ($remote_addr ~* "123.123.123..*|10.10.10..*") {
  20. # set $skip_cache 1;
  21. #}
  22. location ~ [^/]\.php(/|$) {
  23. include enable-php-74.conf; #根据你的PHP版本修改
  24. fastcgi_cache yourname; #此处设置缓存名
  25. fastcgi_cache_bypass $skip_cache;
  26. fastcgi_no_cache $skip_cache;
  27. fastcgi_cache_valid 200 301 302 304 72h;
  28. fastcgi_cache_use_stale error timeout updating invalid_header http_500 http_503;
  29. fastcgi_cache_min_uses 1;
  30. fastcgi_cache_lock on;
  31. fastcgi_hide_header Pragma;
  32. fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
  33. add_header X-Nginx-Cache "$upstream_cache_status";
  34. add_header Last-Modified $date_gmt;
  35. add_header X-Frame-Options SAMEORIGIN;
  36. add_header X-Content-Type-Options nosniff;
  37. add_header X-XSS-Protection "1; mode=block";
  38. etag on;
  39. }
  40. location ~ /purge(/.*) {
  41. allow 127.0.0.1; #允许本机请求清除缓存
  42. #allow 10.10.10.0/24;
  43. #allow fd00:10::/24;
  44. deny all; #除上面允许的IP地址之外,其他IP地址请求一律拒绝。
  45. fastcgi_cache_purge yourname "$scheme$request_method$host$1";#此处设置缓存名
  46. }
  47. #设置js、css等静态文件缓存时间30天
  48. location ~* \.(css|js|pdf)$ {
  49. access_log off;
  50. log_not_found off;
  51. add_header Cache-Control "public, must-revalidate, proxy-revalidate, immutable, max-age=2592000, stale-while-revalidate=86400, stale-if-error=604800";
  52. expires 30d;
  53. }
  54. #设置jpg、png等图片静态文件缓存时间365天
  55. location ~* \.(ogg|ogv|svg|svgz|eot|otf|woff|woff2|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|webp|avif|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
  56. access_log off;
  57. log_not_found off;
  58. add_header Cache-Control "public, must-revalidate, proxy-revalidate, immutable, max-age=31536000, stale-while-revalidate=86400, stale-if-error=604800";
  59. expires 365d;
  60. }
  61. #禁止缓存 robots.txt
  62. location = /robots.txt {
  63. access_log off;
  64. log_not_found off;
  65. add_header Cache-Control "no-cache, no-store, must-revalidate, max-age=0";
  66. expires -1;
  67. }
  68. #禁止缓存 xml 网站地图
  69. location ~* \.(xml|xsl)$ {
  70. access_log off;
  71. log_not_found off;
  72. add_header Cache-Control "no-cache, no-store, must-revalidate, max-age=0";
  73. expires -1;
  74. }
  75. #禁止缓存 wp-cron.php 定时任务
  76. location /wp-cron.php {
  77. add_header Cache-Control "no-cache, no-store, must-revalidate, max-age=0";
  78. expires -1;
  79. }
  80. #此处是我用到的webp图片兼容跳转规则,你们一般用不到可以删除
  81. location ~* ^/?wp-content/uploads/.*\.(?:jpg|jpeg|png)$ {
  82. if ($http_accept !~* "webp"){
  83. return 301 https://r2.yourname.com$request_uri;
  84. break;
  85. }
  86. return 301 https://webp.yourname.com$request_uri;
  87. break;
  88. }
  89. #此处是 Rank Math 插件的 Sitemaps 重写规则,如果没有安装这个插件也可以删除
  90. # START Nginx Rewrites for Rank Math Sitemaps
  91. rewrite ^/sitemap_index.xml$ /index.php?sitemap=1 last;
  92. rewrite ^/([^/]+?)-sitemap([0-9]+)?.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
  93. rewrite ^/([a-z]+)?-sitemap\.xsl$ /index.php?xsl=$1 last;
  94. # END Nginx Rewrites for Rank Math Sitemaps
  95. #此处是 WordPress 默认的伪静态规则
  96. location / {
  97. try_files $uri $uri/ /index.php?$args;
  98. }
  99. rewrite /wp-admin$ $scheme://$host$uri/ permanent;

由于 nginx 缓存没有预生成缓存功能,需要通过访问请求触发生成缓存,所以下面给出个批量预生成缓存的方法。

版权声明:本站部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如本站内容侵犯了您的权益请联系我们,邮箱:1511977125@qq.com 我们核实后会及时处理,发布内容不代表痴痴资讯网立场,本文标题:宝塔面板配置 Nginx Cache 缓存教程本文链接:https://www.chichizixun.com/5202.html