WordPress WP Super Cache 缓存插件 Nginx 优化规则

WP-Super-Cache 作为 WordPress 的老牌静态缓存插件,它在 WordPress.Org 的一个角落一直有一份 Nginx 伪静态规则(https://wordpress.org/support/article/nginx/#wp-super-cache-rules)。
配置后可绕过 PHP 直接由 Nginx 返回 HTML 页面,能大大提高网站的并发能力和速度。
这份规则如下,替换 WordPress 原本的伪静态规则即可(已修改同时兼容 HTTPS/HTTP,并增加是否命中的 Nginx-Static 头)。

# WP Super Cache 规则

set $cache_uri $request_uri;
set $nginx_static 'BYPASS For File';

# POST 请求不读取缓存
if ($request_method = POST)
{
    set $cache_uri 'null cache';
    set $nginx_static 'BYPASS For POST';
}

# 查询请求不读取缓存
if ($query_string != "")
{
    set $cache_uri 'null cache';
    set $nginx_static 'BYPASS For Query';
}

# 特定页面不读取缓存
if ($request_uri ~* "(/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|[a-z0-9_-]+-sitemap ([0-9]+)?.xml)")
{
    set $cache_uri 'null cache';
    set $nginx_static 'BYPASS For URL';
}

# 特定 Cookie 不读取缓存
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in|woocommerce_items_in_cart|woocommerce_cart_hash|wptouch_switch_toogle")
{
    set $cache_uri 'null cache';
    set $nginx_static 'BYPASS For Cookie';
}

# 判断缓存是否存在
if (-f $document_root/wp-content/cache/supercache/$http_host/$cache_uri/index-https.html)
{
    set $nginx_static 'HIT';
}
if (-f $document_root/wp-content/cache/supercache/$http_host/$cache_uri/index.html)
{
    set $nginx_static 'HIT';
}

location /
{
    try_files /wp-content/cache/supercache/$http_host/$cache_uri/index-https.html /wp-content/cache/supercache/$http_host/$cache_uri/index.html $uri $uri/ /index.php?$args;
}

add_header Nginx-Static $nginx_static;
rewrite /wp-admin$ $scheme://$host$uri/ permanent;

保存自用

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索