先来看看官方说明:

root和alias的区别-LMLPHP

root和alias的区别-LMLPHP

root 的用法:

location /request_path/image/ {
root /local_path/;
}

当客户端请求 /request_path/image/cat.png 的时候, nginx把请求映射为/local_path/request_path/image/cat.png。

alias 的用法:

location /request_path/image/ {
alias /local_path/;
}

当客户端请求 /request_path/image/cat.png 的时候, nginx把请求映射为/local_path/cat.png。

总结一下:

  • alias只能位于location下
  • alias指定的目录末尾必须跟 /
  • alias指定的是replacement,注意是replacement就很容易区分和记住了

参考:

http://nginx.org/en/docs/http/ngx_http_core_module.html#root

http://nginx.org/en/docs/http/ngx_http_core_module.html#alias

05-11 20:55