文件路徑 alias與root區別

alias與root區別:

root與alias主要區別在於nginx如何解釋location後面的uri,這會使兩者分別以不同的方式將請求映射到服務器文件上。
root的處理結果是:root路徑+location路徑
alias的處理結果是:使用alias路徑替換location路徑
 
alias是一個目錄別名的定義,root則是最上層目錄的定義。還有一個重要的區別是alias後面必須要用"/"結束,否則會找不到文件的。。。而root則可有可無
 
使用alias標籤的目錄塊中不能使用rewrite的break
 
 

(1)以root方式設置資源路徑    實際路徑是root+請求路徑。

語法:root path;
默認:root html;
配置塊:http、server、location、if
location /download {
    root /etc/nginx;
}

請求的URI爲https://10.21.144.110/download/,web服務器返回/etc/nginx/download/index.html的內容

location /installpack {
    root /etc/nginx;
}

請求的URI爲https://10.21.144.110/installpack/10.21.144.110/diagent_setup32.zip,web服務器返回/etc/nginx/installpack/10.21.144.110/diagent_setup32.zip的內容

 
(2)以alias方式設置資源路徑  實際路徑是alias後的,不包含請求路徑
語法:alias path;
配置塊:location
location /download {
    alias /etc/nginx/download/;
}

請求的URI爲https://10.21.144.110/download/,web服務器返回/etc/nginx/download/index.html的內容

 
 

(3)訪問首頁

語法:index file...;
默認:index index.html;
配置塊:http、server、location
訪問站點的URI是/,這時一般返回首頁,index後可以跟多個文件參數,Nginx會按照順序訪問這些文件
location / {
    root path;
    index.html htmlindex.php 、index.php;;
}
Nginx首先訪問path/index.html,若可以訪問直接返回文件內容結束請求,否則嘗試返回path/htmlindex.php內容,依次類推
 

(4)根據HTTP返回碼重定向

語法:error_page code[code...][=|=answer-code]uri|@named_location
配置塊:http、server、location、if

error_page 404 = @not_found
location @not_found {
      rewrite http://google.com;
}
#上述的作用是如果訪問沒有匹配的url會觸發404指令,然後就匹配到@not_found 這個 location上。
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章