Try_files and index

Why is it my first configuration returns 403 while my second
configuration returns 404?

I looked into logs and it seems my first configuration triggers the
index module which is set to the default index.html, finds nothing and
returns 403. Why isn’t the =404 triggered? And why is the index module
triggered at all?

server {
root /var;
try_files $uri =404;
location / {
}
}

My second configuration works more like I expected; but then, why isn’t
the index module triggered as well? Does try_files disable the index
module somehow?

server {
root /var;
location / {
try_files $uri =404;
}
}

Can someone explain how nginx goes to decide what to do?
Thank you

Posted at Nginx Forum:

On Tue, Jun 26, 2012 at 6:00 AM, whiskybar [email protected] wrote:

try_files $uri =404;
location / {
try_files $uri =404;
}
}

Can someone explain how nginx goes to decide what to do?
Thank you

try_files doesn’t trigger index; you must try $uri/ explicitly.
Therefore, the correct try_files you need is:
try_files $uri $uri/ =404;

I’m afraid I have to disagree on this. That was my guess before I tried
that too. Here’s the relevant part of the log for my first
configuration, with try_files $uri =404 only:

2012/06/25 21:34:22 [debug] 30604#0: *335710 open index
“/var/index.html”
2012/06/25 21:34:22 [debug] 30604#0: *335710 stat() “/var/index.html”
failed (
2: No such file or directory)

And btw, this is on Nginx 1.2 64bit (Ubuntu). Why is it happening?

Posted at Nginx Forum:

Hello!

On Tue, Jun 26, 2012 at 03:18:29AM -0400, whiskybar wrote:

And btw, this is on Nginx 1.2 64bit (Ubuntu). Why is it happening?
In your first configuration try_files is a nop, as it’s defined at
server level and will be only used if request doesn’t match any of
locations defined.

Maxim D.

On Tue, Jun 26, 2012 at 2:18 PM, whiskybar [email protected] wrote:

And btw, this is on Nginx 1.2 64bit (Ubuntu). Why is it happening?

…there’s no /var/index.html ?

Edho A. wrote in post #1066087:

On Tue, Jun 26, 2012 at 2:18 PM, whiskybar [email protected] wrote:

And btw, this is on Nginx 1.2 64bit (Ubuntu). Why is it happening?

…there’s no /var/index.html ?

root should be under location

In your first configuration try_files is a nop, as
it’s defined at
server level and will be only used if request
doesn’t match any of
locations defined.

That explains it then. Thank you very much Maxim!

@Edho there was no /var/index.html because I was forcing a 404 in this
snippet.

Posted at Nginx Forum: