前段时间在做两个nginx实用性的这时候,碰到个难题,请我们先看第二个实用性:
server { listen 80; server_namewww.mos.com; access_log logs/mos.log; error_log logs/mos_error.log debug; root /home/web/www; index index.php; location = /index.php { …… }}
当出访www.mos.com时,和想像中的那样,会前述继续执行 /home/web/www 上面的 index.php。
上面再上看第三个实用性:
server { listen 80; server_namewww.abc.com; index index.html index.php; location / { root /home/web/abc/php/; } location = /index.html { root /home/web/abc/html/; }}
当中:
此次,当出访www.abc.com时,我从前本误以为会继续执行 index.html,但前述继续执行到的看似 index.php。
他们的误会
允诺步入哪两个 location,是依照 request_uri 决定的。由于第二个实用性和他们的预期相符,所以我主观的认为 index 命令实用性的值,会附加到初始的 request_uri 后面,再去寻找 location。
以第二个例子来说,允诺www.mos.com,前述在寻找 location 时,会用 /index.php 去找。但是如果是这样的话,第三个例子在允诺www.abc.com时,应该会用index.html去找,这样就应该继续执行 index.html,但结果却不是这样的。
index命令学习
基于上面的现象,我认识到他们对 index 命令的理解存有难题,所以决定打开手册好好学习一下。
恍然大悟,总结下 index 命令会做的事情如下:
允诺处理逻辑如下:
伪代码如下:
重新分析允诺
为了印证对 index 命令的理解,我重新编译 nginx,并将 error_log 级别设置为 debug,分析www.mos.com允诺,输出如下:
在允诺www.mos.com时,第一次并没有步入到现有的 location 中,之后在 content 阶段继续执行 index 命令,查找到实用性的 index.php 文件存有后,把 request_uri 改为 /index.php 再发起 redirect,最终步入到 location = /index.php{} 中。
有兴趣的话,我们可以自行分析我第三个实用性中www.abc.com允诺会如何做。