1. 程式人生 > 其它 >nginx配置root和alias的區別

nginx配置root和alias的區別

入職新公司 自己配置nginx

開箱即用 不管是本地還是遠端的主機都沒有問題

問題出現在了nginx的配置上

cd到nginx裡面的conf 中 vi nginx.conf

1 2 3 4 location/images{#路徑 root/usr/local/src/test;#指向的資源 autoindex on;#展示目錄 }

  

同時在主機上面對應的路徑 /usr/local/src/test 放了一個test.html 一個zui.jpg

在瀏覽器中開啟 localhost/images/test.htm 或者 伺服器ip/images/zui.jpg

問題來了 404

省略過程,直接說結論

root 指定的目錄 會把匹配的路徑加在 目錄後面 即 我們訪問的資源路徑 其實是 /usr/local/src/test/images/

可並沒有 images這個資料夾 所以報404

解決:

alias 指定目錄 再開啟 localhost/images/test.html 就不會報錯了 (說明:第二行 可以設定 index 或者 autoindex)

1 2 3 4 location/images{#路徑 alias/usr/local/src/test;#指向的資源 indextest.html;#預設資源 }

https://www.cnblogs.com/zui1024/p/13066245.html