nginx的root和alias用法

425 阅读1分钟

root用法:

location ^~/test/{ autoindex on; root /home/angel; }

location ^~  /qw {
    alias  /home/qw/ ;
    index  index.html index.htm;
    try_files $uri $uri/ /index.html;
}

推荐使用alias 这个使用正常访问 当你访问的是/test/index.html 时 会返回主机位置/home/angel/test/index.html

所以不管用root还是alias匹配的域名目录都要与实际最后访问的目录一样,否则会出现部分网页打不开

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中) ^~ /test #匹配之后不在往下匹配