Why does nginx prefixes the location context path to the root location

nginx

I have a simple default website configuration located in my "sites-available" folder in nginx that looks like the one below.

When I try browse to /hello I'd expect it to serve the index.html file located in the root folder I specified. Instead, it is trying to get /hello/index.html within the root location I specified.

Is there a way to tell nginx to serve the files without prefixing the context path?

root /var/...;

location / {
    ...
}

location /hello/ {
    root /home/vagrant/public_html/project/dist;
}

Best Answer

Use alias instead of root; quoting http://nginx.org/en/docs/http/ngx_http_core_module.html#alias

For example, with the following configuration

location /i/ {
    alias /data/w3/images/;
}

on request of “/i/top.gif”, the file /data/w3/images/top.gif will be sent.

Related Question