Nginx not using root from location

Hello. I’m obviously missing something but I’m not quite sure what.

Here’s one of my vhosts.

server {
listen 80;
server_name test.local;

access_log /var/log/nginx/access.log;

root /data/www/htdocs/web;

location / {
index index.php;
}

location /testlocation {
index index.html
root /data/www/htdocs/test;
}
}

When I try to get index.html from /testlocation I always get 404 with
message in errorlog that file is missing in /data/www/htdocs/web. Why?

Posted at Nginx Forum:

Hi Reaper

please check the following:

  1. check the permissions of the folders.
  2. define both root directive in the location.

restart the nginx service and share the results.

Thanks

Shivam

On Tue, Jul 12, 2016 at 10:47:56AM -0400, reaper wrote:

Hi there,

Hello. I’m obviously missing something but I’m not quite sure what.

You don’t have a “root” directive inside the location.

Your “index” directive as-written has three arguments, although you want
it to only have one.

location /testlocation {
index index.html
root /data/www/htdocs/test;
}

Add a semicolon to tell nginx that your “index” directive is complete.

f

Francis D. [email protected]

Yes! That was it. Thank you.

Strange that nginx -t didn’t say anything wrong with config :frowning:

Posted at Nginx Forum:

On Tue, Jul 12, 2016 at 03:47:23PM -0400, reaper wrote:

Hi there,

Yes! That was it. Thank you.

Good stuff.

Strange that nginx -t didn’t say anything wrong with config :frowning:

There’s nothing wrong with the config. It is syntactically correct.

When you request /testlocation/, nginx will look for the
file /data/www/htdocs/web/testlocation/index.html, then the
file /data/www/htdocs/web/testlocation/root, then the file
/data/www/htdocs/web/data/www/htdocs/test, and then fail 404, which is
exactly what you told it to do.

The fact that that is not what you wanted to tell nginx to do, is not
something that nginx can reliably guess. So it doesn’t try.

Cheers,

f

Francis D. [email protected]