# Try\_files behaviour details

**URL:** <https://www.ruby-forum.com/t/try-files-behaviour-details/193385>\
**Category:** NGINX\
**Created:** [August 16, 2010, 10:08pm UTC](https://www.ruby-forum.com/t/try-files-behaviour-details/193385 "2010-08-16T22:08:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ensiferous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e274bd/32.png) [@Ensiferous](https://www.ruby-forum.com/u/Ensiferous)\
**Post date:** [August 16, 2010, 10:08pm UTC](https://www.ruby-forum.com/t/try-files-behaviour-details/193385/1 "2010-08-16T22:08:33Z")

</div>

Hey,

I have a question about how try\_files works in some cases. The standard  
case seems to be

server {  
server\_name foo;

location / {  
try\_files $uri index.html;  
}  
}

Here nginx tries $uri and then finally index.html if no other location  
block matches. Now say that there are no other location blocks and nginx  
enters location / again, the try\_files directive is then skipped as it’s  
already been used once, right?

Second example:

server {  
server\_name bar;

try\_files $uri index.php;  
}

Here try\_files is used in the server block. The wiki says try\_files  
can’t be used here but Nginx will happily accept it, from my testing it  
seems to behave identically to the first example except it’s always  
executed. Is this a correct assumption?

Posted at Nginx Forum:

> **[try\_files behaviour details](https://forum.nginx.org/read.php?2,120447,120447#msg-120447)**
>
> Hey, I have a question about how try\_files works in some cases. The standard case seems to be server { server\_name foo; location / { try\_files $uri index.html; } } Here nginx tries $uri and then finally ind

---

<div class="post-metadata">

**Author:** ![Ensiferous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e274bd/32.png) [@Ensiferous](https://www.ruby-forum.com/u/Ensiferous)\
**Post date:** [August 17, 2010, 1:02am UTC](https://www.ruby-forum.com/t/try-files-behaviour-details/193385/2 "2010-08-17T01:02:59Z")

</div>

Hello!

On Mon, Aug 16, 2010 at 04:07:41PM -0400, Ensiferous wrote:

> }  
> }
> 
> Here nginx tries $uri and then finally index.html if no other location  
> block matches. Now say that there are no other location blocks and nginx  
> enters location / again, the try\_files directive is then skipped as it’s  
> already been used once, right?

No. In the example above nginx will enter location /, test $uri  
existance, if not found - it will issue internal redirect to  
fallback agument, “index.html” in this case.

As “index.html” (invalid uri, may only happen due to internal  
redirects like the above) isn’t matched by location / it will be  
processed in default location context. With default root  
(“/usr/local/nginx/html”) it will try to access  
“/usr/local/nginx/htmlindex.html” file.

> seems to behave identically to the first example except it’s always  
> executed. Is this a correct assumption?

In this example you define try\_files in default location context,  
the one which match both valid uris and “index.php”.

Maxim D.

---

<div class="post-metadata">

**Author:** ![Ensiferous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e274bd/32.png) [@Ensiferous](https://www.ruby-forum.com/u/Ensiferous)\
**Post date:** [August 17, 2010, 1:19am UTC](https://www.ruby-forum.com/t/try-files-behaviour-details/193385/3 "2010-08-17T01:19:27Z")

</div>

## mike Wrote:

> i have try\_files outside of location blocks  
> plenty.
> 
> the wiki probably just needs updating

This was my purpose for asking, I know try\_files works just fine outside  
of location blocks, but I wanted to make sure I fully understood how  
try\_files would work before changing the wiki.

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,120447,120512#msg-120512](http://forum.nginx.org/read.php?2,120447,120512#msg-120512)

---

<div class="post-metadata">

**Author:** ![Ensiferous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e274bd/32.png) [@Ensiferous](https://www.ruby-forum.com/u/Ensiferous)\
**Post date:** [August 17, 2010, 1:13am UTC](https://www.ruby-forum.com/t/try-files-behaviour-details/193385/4 "2010-08-17T01:13:32Z")

</div>

i have try\_files outside of location blocks plenty.

the wiki probably just needs updating
