香港大带宽
特惠
阿里云CN2+精品网线路,可选300M或500M大带宽。年付可享流量翻倍!
注:默认宝塔安装的 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";
如果当前主机有其他站点需要 nginx 缓存,那么第二个站点只需添加上方配置到 http {…} 段落中,然后修改第一行 fastcgi_cache_path、keys_zone 这两项的值即可,最后并把第二行 fastcgi_cache_key 这行注释掉。
#添加到网站配置文件顶部
#http {...}
fastcgi_cache_path /www/wwwroot/www.asiayun.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.asiayun.com asiayun.com;
index index.php index.html index.htm default.php default.htm default.html;
root /wwww/wwwroot/www.asiayun.com;
#SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
#error_page 404/404.html;
ssl_certificate /www/server/panel/vhost/cert/www.asiayun.com/fullchain.pem;
ssl_certificate_key /www/server/panel/vhost/cert/www.asiayun.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.asiayun.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.asiayun.com.conf;
#REWRITE-END
************* 下方省略 **************
将下面的内容添加到宝塔网站设置的伪静态规则中,需修改下方规则中的 asiayun 对应上面你设定的 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 asiayun "$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.asiayun.com$request_uri;
break;
}
return 301 https://webp.asiayun.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.asiayun.com/product_cat-sitemap.xml --output-document - | egrep -o "https?://[^<]+" | wget --output-document=/dev/null -i - >/dev/null 2>&1