http://sudone.com/nginx/nginx_alias.html
如果非要給nginx安上一個虛擬目錄的說法,那就只有alias標簽比較“像”,干脆來說說alias標簽和root標簽的區別吧。
最基本的區別:alias指定的目錄是準確的,root是指定目錄的上級目錄,并且該上級目錄要含有location指定名稱的同名目錄。另外,根據前文所述,使用alias標簽的目錄塊中不能使用rewrite的break。
說不明白,看下配置:
location /abc/ {
alias /home/html/abc/;
}
在這段配置下,http://test/abc/a.html就指定的是/home/html/abc/a.html。這段配置亦可改成
location /abc/ {
root /home/html/;
}
這樣,nginx就會去找/home/html/目錄下的abc目錄了,得到的結果是相同的。
但是,如果我把alias的配置改成:
location /abc/ {
alias /home/html/def/;
}
那么nginx將會從/home/html/def/取數據,這段配置還不能直接使用root配置,如果非要配置,只有在/home/html/下建立一個 def->abc的軟link(快捷方式)了。
一般情況下,在location /中配置root,在location /other中配置alias是一個好習慣。
至于alias和root的區別,我估計還沒有說完全,如果在配置時發現奇異問題,不妨把這兩者換換試試。