Nginx

root和alias

root

以请求 http://example.com/foo/bar/hello.html 为例
匹配到 /foourl的域名+port替换为root指定的目录,即url中的examp.com被替换为了 /home/hfy ,所以实际访问的路径为 /home/hfy/foo/bar/hello.html

location /foo {
    root /home/hfy/;
}

为了更好理解,再来一个例子,请求的url不变,location配置更改为如下,则
匹配到/foo/barurl的域名+port替换为root指定的目录,即url中的examp.com被替换为了/home/hfy,所以实际访问的路径仍然为/home/hfy/foo/bar/hello.html。root在替换时不会替换匹配到的路径。

location /foo/bar {
    root /home/hfy/;
}

alias

以请求http://example.com/foo/bar/hello.html为例,location配置如下.
匹配到/foo,url的ip/域名+port+匹配到的路径替换为alias指定的目录,即url中的example.com/foo被替换为了/home/hfy,所以实际访问的路径为/home/hfy/bar/hello.html

location /foo {
    alias /home/hfy/;
}

同样再来一个例子,请求的url不变,如果location配置更改为如下,则
匹配到/foo/bar,url的ip/域名+port+匹配到的路径替换为alias指定的目录,即url中的example.com/foo/bar被替换为了/home/hfy,所以实际访问的路径为/home/hfy/hello.html。alias在替换时会替换匹配到的路径。 alias其余特性,最左匹配、index、location解析url工作流程、末尾’/'与root一致。

location /foo/bar {
    alias /home/hfy/;
}

参考这里!