Nginx root&alias文件路徑配置解析
- nginx在指定文件路徑有兩種方式root和alias,這兩種的主要區別在於nginx如何解析location後面的uri,這會使兩者分別以不同的方式請求映射到服務器的文件上。
1.root語法的使用
【root】
語法: root path
默認值: root html
配置段: http/server/location/if
例子:
location ^~/chen/ {
root /data/www/www.chen.com;
autoindex on;
auto_basic "Restricted";
auto_basic_user_file passwd/chen;
}
例子解析:
如果請求的uri是/chen/httplogs/www.chen.com-access.log時,web服務器將會返回服務器上的“/data/www/www.chen.com”(root的path)+“/chen/httplogs/www.chen.com-access.log”的文件。也就是說root路徑配置會根據完整的uri請求來映射,也就是/path/uri
2.alias語法的使用
【alias】
語法:alias path
配置段:location
例子:
location ^~ /binapp/ {
limit_conn limit 4;
limit_rate 200k;
internal;
alias /data/statics/bin/apps/;
}
例子解析:
alias會把location後面配置的路徑丟棄,把當前匹配到的目錄指向到指定的目錄。如果一個請求的uri是/binapp/a.chen.com/favicon時,web服務器將會返回服務器上的“/data/statics/bin/apps/”+“a.chen.com/favicon.html”的文件。
alias使用總結:
- 使用alias時,目錄名後面一定要加“/”
- alias可以指定任何名稱
- alias在使用正則表達式時,必須捕捉要匹配到的內容並在指定的內容處使用
- alias只能位於location塊中。