5

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;
}
  • You do know that nginx looks at the sites-enabled directory and not sites-available? There should be a symlink from the former to the latter for those sites that should be enabled. Perhaps you've edited a config that's not enabled and hence not being picked up. – wurtel Nov 14 '14 at 9:18
  • Yes, I should have mentioned that. As practice, I always work on sites-available and symlink what's ready on sites-enabled (which is what I did this case). – Pablo Nov 16 '14 at 21:08
  • 1
    Did you try browsing to /hello as you write, or to /hello/ as the location line specifies? (Note the trailing slash in the latter.) – wurtel Nov 17 '14 at 7:45
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.