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

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

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

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

#http {...}
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;#此处设置缓存名
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 这行注释掉。

参考案例:


#添加到网站配置文件顶部
#http {...}
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;#此处设置缓存名
fastcgi_cache_key "$scheme$request_method$host$request_uri";

server
{
    listen 80 reuseport;
    listen 443 ssl http2 reuseport;
    listen [::]:443 ssl http2 reuseport;
    listen [::]:80 reuseport;
    server_name www.yourname.com yourname.com;
    index index.php index.html index.htm default.php default.htm default.html;
    root /wwww/wwwroot/www.yourname.com;

    #SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
    #error_page 404/404.html;
    ssl_certificate    /www/server/panel/vhost/cert/www.yourname.com/fullchain.pem;
    ssl_certificate_key    /www/server/panel/vhost/cert/www.yourname.com/privkey.pem;
    ssl_early_data on;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    add_header Strict-Transport-Security "max-age=31536000";
    error_page 497  https://$host$request_uri;
    #SSL-END
    #引用重定向规则,注释后配置的重定向代理将无效
    include /www/server/panel/vhost/nginx/redirect/www.yourname.com/*.conf;

    #ERROR-PAGE-START  错误页配置,可以注释、删除或修改
    #error_page 404 /404.html;
    #error_page 502 /502.html;
    #ERROR-PAGE-END

    #PHP-INFO-START  PHP引用配置,可以注释或修改
    #include enable-php-74.conf;#重点!注销宝塔默认配置!
    #PHP-INFO-END

    #REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效
    include /www/server/panel/vhost/rewrite/www.yourname.com.conf;
    #REWRITE-END
    ************* 下方省略 **************

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

#server {...}
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/|/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)") {
    set $skip_cache 1;
}  
#如果携带 cookie 包含下方内容则跳过
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") {
    set $skip_cache 1;
}
#添加可以绕过缓存的终端IP地址
#if ($remote_addr ~* "123.123.123..*|10.10.10..*") { 
#    set $skip_cache 1; 
#}
location ~ [^/]\.php(/|$) {
include enable-php-74.conf; #根据你的PHP版本修改
fastcgi_cache yourname; #此处设置缓存名
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
fastcgi_cache_valid 200 301 302 304 72h;
fastcgi_cache_use_stale error timeout updating invalid_header http_500 http_503;
fastcgi_cache_min_uses 1;
fastcgi_cache_lock on;
fastcgi_hide_header Pragma;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
add_header X-Nginx-Cache "$upstream_cache_status";
add_header Last-Modified $date_gmt;
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
etag on;
}

location ~ /purge(/.*) {
    allow 127.0.0.1; #允许本机请求清除缓存
    #allow 10.10.10.0/24;
    #allow fd00:10::/24;
    deny all; #除上面允许的IP地址之外,其他IP地址请求一律拒绝。
    fastcgi_cache_purge yourname "$scheme$request_method$host$1";#此处设置缓存名
}  
#设置js、css等静态文件缓存时间30天
location ~* \.(css|js|pdf)$ { 
    access_log off; 
    log_not_found off; 
    add_header Cache-Control "public, must-revalidate, proxy-revalidate, immutable, max-age=2592000, stale-while-revalidate=86400, stale-if-error=604800"; 
    expires 30d;
}
#设置jpg、png等图片静态文件缓存时间365天
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)$ {
    access_log off; 
    log_not_found off; 
    add_header Cache-Control "public, must-revalidate, proxy-revalidate, immutable, max-age=31536000, stale-while-revalidate=86400, stale-if-error=604800"; 
    expires 365d;
}
#禁止缓存 robots.txt 
location = /robots.txt {
    access_log off; 
    log_not_found off; 
    add_header Cache-Control "no-cache, no-store, must-revalidate, max-age=0";
    expires -1;
}
#禁止缓存 xml 网站地图
location ~* \.(xml|xsl)$ { 
    access_log off; 
    log_not_found off; 
    add_header Cache-Control "no-cache, no-store, must-revalidate, max-age=0"; 
    expires -1;
}
#禁止缓存 wp-cron.php 定时任务
location /wp-cron.php { 
    add_header Cache-Control "no-cache, no-store, must-revalidate, max-age=0"; 
    expires -1; 
}
#此处是我用到的webp图片兼容跳转规则,你们一般用不到可以删除
location ~* ^/?wp-content/uploads/.*\.(?:jpg|jpeg|png)$ {
    if ($http_accept !~* "webp"){
        return 301 https://r2.yourname.com$request_uri;
        break;
    }
    return 301 https://webp.yourname.com$request_uri;
    break;
}
#此处是 Rank Math 插件的 Sitemaps 重写规则,如果没有安装这个插件也可以删除
# START Nginx Rewrites for Rank Math Sitemaps
rewrite ^/sitemap_index.xml$ /index.php?sitemap=1 last;
rewrite ^/([^/]+?)-sitemap([0-9]+)?.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
rewrite ^/([a-z]+)?-sitemap\.xsl$ /index.php?xsl=$1 last;
# END Nginx Rewrites for Rank Math Sitemaps
#此处是 WordPress 默认的伪静态规则
location / {
    try_files $uri $uri/ /index.php?$args;
}
rewrite /wp-admin$ $scheme://$host$uri/ permanent;

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

添加下方命令到宝塔面板定时任务中,任务类型为 shell 脚本。
下方命令的意思是通过 wget 访问站点地图 sitemap.xml 内的所有链接,从而达到生成 nginx 缓存的目的。

wget --quiet https://www.yourname.com/product_cat-sitemap.xml --output-document - | egrep -o "https?://[^<]+" | wget --output-document=/dev/null -i - >/dev/null 2>&1

给TA打赏
共{{data.count}}人
人已打赏
WordPress教程

解决WordPress MySQL 数据库中产生的巨型表 wp_actionscheduler_actions 和 wp_actionscheduler_logs

2021-12-24 23:48:13

Docker容器

分享一个自编译 Docker 容器 Guacamole - 基于 Web 的 VNC 客户端

2020-10-7 18:52:47

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索