root用法:
location ^~/test/{
autoindex on;
root /home/angel;
}
当你访问的是/test/index.html 时 会返回主机位置/home/angel/test/index.html
alias用法:
location ^~/test/ {
autoindex on;
alias /home/angel/new/; #注意这里的new和上面没有的
}
当你访问的是/test/index.html 时 会返回主机位置/home/angel/new/index.html
注意:
- 一定要注意 alias 的目录一定要以 / 结尾不然会导致400错误
实际上alias的作用是替换请求中被匹配的url
即通过location 匹配到的规则通过 alias取别名映射成alias所指定的目录,后面的url会和alias设置的 自动拼接,找到主机中该位置的资源 - alias在使用正则匹配时,必须捕捉要匹配的内容并在指定的内容处使用。
- alias只能位于location块中。(root可以不放在location中)