nginx中root和alias的區別
今天使用nginx搭建了一個網站,訪問后出現404錯誤Not found. 上網查了一下原因,是由於nginx的配置不對。因為我是有兩個web目錄,這兩個目錄在不同的位置上。而且我不想把兩個目錄合並在一起,所以就要配置兩個location。配置如下:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
index index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
location / {
root /www;
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location /website/ {
root /var/lib/www;
autoindex on;
}
}
上面的配置瀏覽http://localhost/website/會顯示404錯誤,因為root屬性指定的值是要加入到最終路徑的,所以訪問的位置變成了/var/lib/www/website/
。而我不想把訪問的URI加入到路徑中。所以就需要使用alias屬性,其會拋棄URI,直接訪問alias指定的位置, 所以最終路徑變成/var/lib/www/
。(最后需要加斜線)
location /website/ {
alias /var/lib/www;
autoindex on;
}
@完