Nginx中root和alias的区别

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;
    }

你可能感兴趣的:(Nginx中root和alias的区别)