1. 程式人生 > >Nginx配置中的 root 與 alias 指令的區別

Nginx配置中的 root 與 alias 指令的區別

root和alias都可以定義在location模組中,都是用來指定請求資源的真實路徑,比如:

location /i/ {
    root /data/w3;
}

請求 http://foofish.net/i/top.gif 這個地址時,那麼在伺服器裡面對應的真正的資源是 /data/w3/i/top.gif檔案,注意真實的路徑是root指定的值加上location指定的值root

而 alias 正如其名,alias指定的路徑是location的別名,不管location的值怎麼寫,資源的真實路徑都是 alias 指定的路徑,比如:

location /i/ {
    alias /data/w3/;
}

同樣請求 http://foofish.net/i/top.gif 時,在伺服器查詢的資源路徑是:/data/w3/top.gif

alias

其他區別:

  1. alias 只能作用在location中,而root可以存在server、http和location中。
  2. alias 後面必須要用 “/” 結束,否則會找不到檔案,而 root 則對 ”/” 可有可無。

參考:

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

關注公眾號「Python之禪」(id:vttalk)獲取最新文章 python之禪