# Try\_files and index

**URL:** <https://www.ruby-forum.com/t/try-files-and-index/220943>\
**Category:** NGINX\
**Created:** [June 26, 2012, 1:01am UTC](https://www.ruby-forum.com/t/try-files-and-index/220943 "2012-06-26T01:01:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [June 26, 2012, 1:01am UTC](https://www.ruby-forum.com/t/try-files-and-index/220943/1 "2012-06-26T01:01:28Z")

</div>

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:

> **[try\_files and index](https://forum.nginx.org/read.php?2,227887,227887#msg-227887)**
>
> 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...

---

<div class="post-metadata">

**Author:** ![whiskybar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/50afbb/32.png) [@whiskybar](https://www.ruby-forum.com/u/whiskybar)\
**Post date:** [June 26, 2012, 3:46am UTC](https://www.ruby-forum.com/t/try-files-and-index/220943/2 "2012-06-26T03:46:51Z")

</div>

On Tue, Jun 26, 2012 at 6:00 AM, whiskybar [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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;

---

<div class="post-metadata">

**Author:** ![whiskybar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/50afbb/32.png) [@whiskybar](https://www.ruby-forum.com/u/whiskybar)\
**Post date:** [June 26, 2012, 9:19am UTC](https://www.ruby-forum.com/t/try-files-and-index/220943/3 "2012-06-26T09:19:19Z")

</div>

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:

> **[Re: try\_files and index](https://forum.nginx.org/read.php?2,227887,227909#msg-227909)**
>
> 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...

---

<div class="post-metadata">

**Author:** ![whiskybar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/50afbb/32.png) [@whiskybar](https://www.ruby-forum.com/u/whiskybar)\
**Post date:** [June 26, 2012, 9:32am UTC](https://www.ruby-forum.com/t/try-files-and-index/220943/4 "2012-06-26T09:32:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![whiskybar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/50afbb/32.png) [@whiskybar](https://www.ruby-forum.com/u/whiskybar)\
**Post date:** [June 26, 2012, 9:23am UTC](https://www.ruby-forum.com/t/try-files-and-index/220943/5 "2012-06-26T09:23:24Z")

</div>

On Tue, Jun 26, 2012 at 2:18 PM, whiskybar [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

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

---

<div class="post-metadata">

**Author:** ![whiskybar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/50afbb/32.png) [@whiskybar](https://www.ruby-forum.com/u/whiskybar)\
**Post date:** [June 26, 2012, 9:32am UTC](https://www.ruby-forum.com/t/try-files-and-index/220943/6 "2012-06-26T09:32:58Z")

</div>

Edho A. wrote in post #1066087:

> On Tue, Jun 26, 2012 at 2:18 PM, whiskybar [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![whiskybar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/50afbb/32.png) [@whiskybar](https://www.ruby-forum.com/u/whiskybar)\
**Post date:** [June 26, 2012, 9:47am UTC](https://www.ruby-forum.com/t/try-files-and-index/220943/7 "2012-06-26T09:47:57Z")

</div>

> 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:  
[http://forum.nginx.org/read.php?2,227887,227915#msg-227915](http://forum.nginx.org/read.php?2,227887,227915#msg-227915)
