从零学nginx-root,alias,index
1. root
root模块就是用来指定访问资源的document_root,nginx会将root的值和location的值进行拼接,然后再拼接上url剩余的部分及index指定的值,举例:
location /a/{
root /usr/local;
index 1.html;
}
访问结果如下:
/a --> /usr/local/a/1.html
/a/b --> /usr/local/a/b/1.html
2. alias
alias模块也是用来指定访问资源的document_root,但是nginx会将alias的值替换location的值,然后再拼接上url剩余的部分及index指定的值,举例:
location /a/{
alias /usr/local;
index 1.html;
}
访问结果如下:
/a --> /usr/local/1.html
/a/b --> /usr/local/b/1.html
alias模块有点需要注意:alias指定的是document_root,不能直接指定到具体的文件,否则会500,例如:
location /a/{
alias /usr/local/1.html;
#index 1.html;
}
这种配置会让nginx报出500
3. index
通过上面的两个例子,我们知道index就是用来指定具体的资源的名字的,index可以指定多个资源的名字,第一个匹配不到,就会匹配第二个,直到匹配到为止。
但是location使用严格匹配的时候,不能使用index,否则会导致root失效,如:
location =/a/{
root /usr/local/;
index 1.html;
}
当访问的url为/a/时,实际访问的资源为:nginx的默认document_root/a/1.html,使得root失效,值得注意的是nginx的默认document_
本文参考链接:https://unclewang.blog.csdn.net/article/details/115049808
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。