半个月前做了专属伪静态规则优化:基于WordPress配合缓存插件WP Super Cache和子比模板的Nginx伪静态规则。
可惜公司不给我配专属测试员,私货不给用。。。最近发现/user 提示404错误,会员中心坏了其不是要坏事?赶紧修,我自己搞得,绝对没用公司程序猿!
# ======================================================
# WordPress WP Super Cache + 子比主题终极优化伪静态规则
# 修复 /user 404 问题 | 完全兼容 Nginx 1.26+
# ======================================================
# 核心缓存控制变量
set $supercache_skip 0;
# 动态请求排除条件
if ($request_method = POST) { set $supercache_skip 1; }
if ($query_string != "") { set $supercache_skip 1; }
if ($http_cookie ~* "(comment_author|wordpress_logged_in|wp_postpass|woocommerce_cart_hash)") {
set $supercache_skip 1;
}
# 子比主题动态路径排除 - 增强正则匹配
if ($request_uri ~* "(^/user/|^/pay/|^/order/|^/action/|^/msg/|^/credit/|^/sign|^/login|^/register|^/resetpass|^/add-favorite|^/like-post|^/comment-upvote|^/withdraw|/ajax\.php|^/notifications)") {
set $supercache_skip 1;
}
# 子比主题专用路由处理 - 解决 /user 404 问题
location ~ ^/(user|pay|order|action|msg|credit|sign|login|register|resetpass|add-favorite|like-post|comment-upvote|withdraw|notifications) {
# 确保直接交给 WordPress 处理
try_files $uri $uri/ /index.php?$args$is_args$args;
}
# 主处理块 - 单一 try_files 指令
location / {
# 尝试提供超级缓存文件或回退到 WordPress
try_files /wp-content/cache/supercache/$http_host/$uri/index.html $uri $uri/ @nocache;
}
# 缓存跳过处理
location @nocache {
try_files $uri $uri/ /index.php?$args;
}
# ========================
# 增强功能配置
# ========================
# 静态资源长期缓存
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|webp|woff2|woff|ttf)$ {
expires 365d;
add_header Cache-Control "public, immutable";
add_header Access-Control-Allow-Origin "*"; # 解决字体跨域问题
access_log off;
}
# 管理后台重定向
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
# 安全设置 - 屏蔽敏感文件
location ~* (wp-config\.php|readme\.html|license\.txt|\.htaccess) {
deny all;
access_log off;
}
# 禁止访问隐藏文件
location ~ /\.(?!well-known) {
deny all;
access_log off;
log_not_found off;
}
# 防止缓存动态内容
location ~* (wp-admin|wp-login|user|pay|action|msg|ajax\.php|notifications) {
expires off;
add_header Cache-Control "no-cache, no-store, must-revalidate";
}
# 子比主题API端点保护
location ~ ^/(zibpay|action) {
# 确保直接交给 WordPress 处理
try_files $uri $uri/ /index.php?$args$is_args$args;
}
# 解决子比主题点赞/收藏功能
location ~ ^/(like-post|add-favorite) {
try_files $uri $uri/ /index.php?$args$is_args$args;
}
没有回复内容