Thursday, April 25, 2024

nginx 虛擬目錄的配置

nginx貌似沒有虛擬目錄的說法,因為它本來就是完完全全根據目錄來設計並工作的。

如果非要給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的區別,我估計還沒有說完全,如果在配置時發現奇異問題,不妨把這兩者換換試試。

原文鏈接:http://www.sudone.com/nginx/nginx_alias.html

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.