# Use try\_files and still get errors 404

**URL:** <https://www.ruby-forum.com/t/use-try-files-and-still-get-errors-404/208246>\
**Category:** NGINX\
**Created:** [June 13, 2011, 11:11am UTC](https://www.ruby-forum.com/t/use-try-files-and-still-get-errors-404/208246 "2011-06-13T11:11:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [June 13, 2011, 11:11am UTC](https://www.ruby-forum.com/t/use-try-files-and-still-get-errors-404/208246/1 "2011-06-13T11:11:04Z")

</div>

Hello,

recently I migrate one of domains, I manage, to use try\_files directive  
instead of several “if” blocks and rewrites. New server description is  
like this:

server {  
listen X.X.X.X;  
server\_name [example.com](http://example.com) \*.example.com;

index index.php;  
root /www/example.com/www/root;

try\_files $uri $uri/ /index.php$is\_args$args;

location ~\* .(jpg|jpeg|gif|png|ico|swf)$ {  
gzip off;  
expires 7d;  
}  
location ~\* .(js|css)$ {  
expires 7d;  
}  
location ~ .php$ {  
fastcgi\_pass upstreanphp;  
fastcgi\_param SCRIPT\_FILENAME $document\_root$fastcgi\_script\_name;  
fastcgi\_intercept\_errors on;  
fastcgi\_read\_timeout 120;  
fastcgi\_next\_upstream error timeout invalid\_header;  
include fastcgi\_params;  
}  
}

Strange thing (for me) is if I try some URL like  
[http://example.com/path/to/non/existing/file.jpg](http://example.com/path/to/non/existing/file.jpg) (or any other extension  
from location checks) I receive error 404. Is this normal behavior ? I  
expect index.php to be open. I’ve tried to put try\_files in location / {  
} , but effect is the same ? Do I have to put try\_files in every  
location block ? Or this is some kind of bug ?

I use nginx 0.8.54 on linux x86\_64

Greetings,  
Hristo

Posted at Nginx Forum:

> **[Use try\_files and still get errors 404](https://forum.nginx.org/read.php?2,206358,206358#msg-206358)**
>
> Hello, recently I migrate one of domains, I manage, to use try\_files directive instead of several "if" blocks and rewrites. New server description is like this: server { listen X.X.X.X; server\_name example.com \*.example.com; i

---

<div class="post-metadata">

**Author:** ![iko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/9e8a1a/32.png) [@iko](https://www.ruby-forum.com/u/iko)\
**Post date:** [June 13, 2011, 10:45pm UTC](https://www.ruby-forum.com/t/use-try-files-and-still-get-errors-404/208246/2 "2011-06-13T22:45:17Z")

</div>

Hello!

On Mon, Jun 13, 2011 at 05:10:19AM -0400, iko wrote:

> index index.php;  
> }  
> Strange thing (for me) is if I try some URL like  
> [http://example.com/path/to/non/existing/file.jpg](http://example.com/path/to/non/existing/file.jpg) (or any other extension  
> from location checks) I receive error 404. Is this normal behavior ? I  
> expect index.php to be open. I’ve tried to put try\_files in location / {  
> } , but effect is the same ? Do I have to put try\_files in every  
> location block ? Or this is some kind of bug ?

Directive try\_files isn’t inhereted from previous levels, and not  
expected to. You’ve defined it at server level (which isn’t  
allowed per documentation, btw, but happens to work - this is  
probably a bug, indeed) and this results in try\_files being used  
in an implicit location.

There are no try\_files in other locations though, and the result  
you see is expected. If you want try\_files to be in other  
locations - you have to place it there too.

Maxim D.

---

<div class="post-metadata">

**Author:** ![iko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/9e8a1a/32.png) [@iko](https://www.ruby-forum.com/u/iko)\
**Post date:** [June 14, 2011, 10:06am UTC](https://www.ruby-forum.com/t/use-try-files-and-still-get-errors-404/208246/3 "2011-06-14T10:06:41Z")

</div>

Hello, Maxim,

thanks for reply. Now I understand better try\_files directive and it  
really should be better described in documentation/wiki. At least is it  
supports server section and how.

Hristo

Posted at Nginx Forum:

> **[Re: Use try\_files and still get errors 404](https://forum.nginx.org/read.php?2,206358,206660#msg-206660)**
>
> Hello, recently I migrate one of domains, I manage, to use try\_files directive instead of several "if" blocks and rewrites. New server description is like this: server { listen X.X.X.X; server\_name example.com \*.example.com; i
