ginx指定文件路徑有兩種方式root和alias,這兩者的用法區別,使用方法總結了下,方便大家在應用過程中,快速響應。
root與alias主要區別在於nginx如何解釋location后面的uri,這會使兩者分別以不同的方式將請求映射到服務器文件上。
[root]
語法:root path
默認值:root html
配置段:http、server、location、if
[alias]
語法:alias path
配置段:location
location ^~ /t/ {
root /www/root/html/; # 如果一個請求的URI是/t/a.html時,web服務器將會返回服務器上的/www/root/html/t/a.html的文件
}
# alias會把location后面配置的路徑丟棄掉,把當前匹配到的目錄指向到指定的目錄
location ^~ /t/ {
alias /www/root/html/new_t/; # 如果一個請求的URI是/t/a.html時,web服務器將會返回服務器上的/www/root/html/new_t/a.html的文件
}
注意:
- 使用alias時,目錄名后面一定要加"/"。
- alias在使用正則匹配時,必須捕捉要匹配的內容並在指定的內容處使用。
- alias只能位於location塊中。(root可以不放在location中)