面板版本:BT-Panel 6.8.11 Beta
系统版本:CentOS 7.6 1810
浏览器版本:Google Chrome 73.0.3664.3
问题现象:Nginx WordPress 默认 Rewrite 规则有误
重现方式:添加默认 WordPress 重写规则(错得太明显了,你只要添加一下就看见了)
问题详情:
其实这个问题,我从去年就在说,宝塔也曾经改过,但至今没改过来,匪夷所思。现在宝塔的 Nginx 环境下,默认的 WordPress 重写规则是这样的:
location / {
index index.html index.php;
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
}
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
我们可以来分析一下,这个规则首先调整了 index 顺序,把 index.html 放在了 index.php 之前。而地球人都知道,WordPress 的入口文件是 index.php,因此这个规则显而易见是错的。
然后,这个规则下面几行做了什么事情呢?唯一的一件事就是隐藏各种index,但 WordPress 默认并没有 index.html。那么,宝塔为何会采用这么奇怪的规则呢?
我上谷歌百度了一下,原来,这个规则源头是2013年的一篇博客文章:https://wayne173.iteye.com/blog/1913862
博主大概是刚刚从 Apache 迁移到 Nginx,一口气写了一大堆规则,WordPress 只是其中之一。彼时 Apache 仍是主流,这么“全面”的“技术帖”引得国内站长纷纷转载。但问题是,他根目录的 index.html 可能是个网站建设中的静态页面,他写配置的时候大概忘了删,你就算要照抄,起码也要把 index index.html index.php; 这句去掉,不然也太糊弄事了。
那么,正确的,或者是通用性更高的 WordPress 重写规则到底是什么呢?
这个在 WordPress 官方文档 和 Nginx 官方文档都有详细介绍,是下面这样的:
location / {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /index.php?$args;
}
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
看,就这么短,其中还有两行是注释,也就是说,规则其实只有两行!
我们不妨看看这两行注释是什么:
# 这个规则很酷,因为静态内容完全不触及PHP
# 包含 "?$args" 目的是,即使使用非默认的固定链接,也不会在使用查询字段时被破坏
以上内容翻译自
|
|