今天使用nginx搭建了一個網站,訪問後出現404錯誤Not found. 上網查了一下緣由,是因爲nginx的配置不對。由於我是有兩個web目錄,這兩個目錄在不一樣的位置上。並且我不想把兩個目錄合併在一塊兒,因此就要配置兩個location。配置以下:html
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/。(最後須要加斜線)nginx
location /website/ { alias /var/lib/www; autoindex on; }
@完web
參考:http://blog.csdn.net/u011510825/article/details/50531864網站