今天使用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网站