nginx 配置檔案

~技术小白發表於2024-10-08
server {
 listen 80 default_server;
 server_name www.example.com;
 location / {
 root /usr/share/nginx/html;
 # alias /usr/share/nginx/html;
 index index.html index.htm;
 }
}

root和alisa的區別

root指令定義了處理位於根路徑(即"/")下的請求時,伺服器應該從哪個目錄下查詢檔案或目錄。這裡的root /usr/share/nginx/html;表示任何對www.example.com的請求都會到/usr/share/nginx/html這個目錄下去查詢對應的資源。
alias指令與root指令類似,但它會在給定的路徑前加上location匹配的URI。這意味著如果你使用alias而不是root,並且location有指定的字首,那麼這個字首不會被再次新增到請求的URI上。而使用root時,location的字首會被重複新增兩次。
例如:
如果使用root並且有非空的location字首,比如location /images/,對於一個請求http://www.example.com/images/pic.jpg,Nginx將會嘗試從/usr/share/nginx/html/images/images/pic.jpg查詢檔案,因為/images/這個字首被加了兩次。

但如果使用alias,同樣的請求http://www.example.com/images/pic.jpg,Nginx將會正確地從/usr/share/nginx/html/images/pic.jpg查詢檔案

相關文章