【每日阅读】2020年10月7日-wordpress的nginx rewrite逻辑解析

localtion匹配规则

参考:http://nginx.org/en/docs/http/request_processing.html

nginx匹配请求该被哪个location处理时,使用的是精确匹配原则,哪个location最精确就使用哪个location。没有匹配到时使用location /的处理块。

所以下面配置解析中的location /块才会处理那些非静态资源。

解析rewrite逻辑

参考:https://www.lyblog.net/detail/600.html

if条件含义

参数含义
-f文件存在为true
!-f文件不存在为true
location / {
    # 文章链接的rewrite逻辑
    # 如果不存在这个资源,就请求index.php去处理
    # 例如本片文章请求的是/archives/2558,根本不存在这个文件啊,就会在nginx内部重写这个请求,去请求index.php,php的请求被wordpress接管,所以就处理ok了。
    # 但是如果请求的是/abc.js,这种静态文件都是存在的啊,所以就不重写请求,wordpress的index.php不会接管
    if (!-f $request_filename){
        rewrite (.*) /index.php;
    }
​
    # 少了这个,进入/wp-admin页面会死循环
    # 死循环就是因为/wp-admin时,发现/wp-admin是不存在的文件,所以被wordpress接管
    # 接管后,wordpress返回302重定向,wordpress认为这个地址是可以请求的,所以又让浏览器请求/wp-admin,所以就死循环了
    # 而实际上wordpress的内部是存在/wp-admin/index.php文件的,所以应该由wordpress去处理/wp-admin/index.php这个资源
    if (-f $request_filename/index.php){
        rewrite (.*) $1/index.php;
    }
}

重定向的截图如下

【每日阅读】2020年10月7日-wordpress的nginx rewrite逻辑解析

原创文章,作者:geekgao,如若转载,请注明出处:https://www.geekgao.cn/archives/2558

(1)
geekgaogeekgao博主
上一篇 2020年10月6日 下午10:12
下一篇 2020年10月9日 上午5:56

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

GitHub
分享本页
返回顶部

Warning: error_log(/usr/local/lighthouse/softwares/wordpress/wp-content/plugins/spider-analyser/#log/log-1705.txt): failed to open stream: No such file or directory in /usr/local/lighthouse/softwares/wordpress/wp-content/plugins/spider-analyser/spider.class.php on line 2900