# Quick question about try\_files

**URL:** <https://www.ruby-forum.com/t/quick-question-about-try-files/201461>\
**Category:** NGINX\
**Created:** [January 7, 2011, 8:43pm UTC](https://www.ruby-forum.com/t/quick-question-about-try-files/201461 "2011-01-07T20:43:19Z")\
**Posts on this page:** 5\
**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:** [January 7, 2011, 8:43pm UTC](https://www.ruby-forum.com/t/quick-question-about-try-files/201461/1 "2011-01-07T20:43:19Z")

</div>

Hello, all.

nginx version: nginx/0.7.67

This works great to look first in fast local storage, and then fall back  
to  
slower storage for older archives:

```
location ^~ /providers/ {
  error_page 404 = @archives;
  log_not_found off;
}
location @archives {
  rewrite "/providers/(.*)" /archives/providers/$1 last;
}

```

I thought I could tidy this up by doing the following, but it doesn’t  
work:

```
location ^~ /providers/ {
  try_files $uri /archives$uri @mongrel_app;
}

```

Anyone see why? Thanks in advance.

Aaron

---

<div class="post-metadata">

**Author:** ![ktheory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@ktheory](https://www.ruby-forum.com/u/ktheory)\
**Post date:** [January 7, 2011, 10:04pm UTC](https://www.ruby-forum.com/t/quick-question-about-try-files/201461/2 "2011-01-07T22:04:49Z")

</div>

Hello!

On Fri, Jan 07, 2011 at 11:41:06AM -0800, Aaron Starr wrote:

> ```
> }
> location @archives {
> rewrite "/providers/(.*)" /archives/providers/$1 last;
> }
> 
> ```

This rewrites to /archives/… and searches for location again.  
Request will be handled elsewhere, and it’s not known how it will  
be handled from the config snippet provided.

> I thought I could tidy this up by doing the following, but it doesn’t work:
> 
> ```
> location ^~ /providers/ {
> try_files $uri /archives$uri @mongrel_app;
> }
> 
> ```
> 
> Anyone see why? Thanks in advance.

This should work as long as root for archives matches one for  
providers. But won’t work as long as you have something like

```
location ^~ /archives/ {
    root /path/to/another/storage;
}

```

in your config.

Maxim D.

---

<div class="post-metadata">

**Author:** ![ktheory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@ktheory](https://www.ruby-forum.com/u/ktheory)\
**Post date:** [January 7, 2011, 10:28pm UTC](https://www.ruby-forum.com/t/quick-question-about-try-files/201461/3 "2011-01-07T22:28:42Z")

</div>

Maxim,

Thanks for your quick reply!

I should have included that in both cases, the default handler is this:

```
location / {
  try_files $uri $uri/index.html @mongrel_app;
}

```

And, there is no /archives/ handler. So, here are the snippets that I  
think  
are relevant. Working:

```
location ^~ /providers/ {
  error_page 404 = @archives;
  log_not_found off;
}
location @archives {
  rewrite "/providers/(.*)" /archives/providers/$1 last;
}
location / {
  try_files $uri $uri/index.html @mongrel_app;
}

```

Not working:

```
location ^~ /providers/ {
  try_files $uri /archives$uri @mongrel_app;
}
location / {
  try_files $uri $uri/index.html @mongrel_app;
}

```

Also, I should mention that it in the not-working case, the behavior is  
that  
it will correctly find the file in /providers/… but if the file is  
moved  
to /archives/providers/…, it will no longer find it.

I’ve put all the location directives here: [http://pastie.org/1438414](http://pastie.org/1438414)

Thanks, again,

Aaron

---

<div class="post-metadata">

**Author:** ![ktheory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@ktheory](https://www.ruby-forum.com/u/ktheory)\
**Post date:** [January 8, 2011, 3:36am UTC](https://www.ruby-forum.com/t/quick-question-about-try-files/201461/4 "2011-01-08T03:36:49Z")

</div>

Maxim,

Again, thanks for your help. I created the debug log and found a  
permissions  
problem.

I had dismissed permissions as a possible reason for the behavior,  
because  
it was working fine with the one configuration (I promise!). But,  
whatever.  
A couple of permissions changes and voila! Everything works as expected.

I sincerely appreciate your help.

Aaron

---

<div class="post-metadata">

**Author:** ![ktheory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@ktheory](https://www.ruby-forum.com/u/ktheory)\
**Post date:** [January 8, 2011, 2:18am UTC](https://www.ruby-forum.com/t/quick-question-about-try-files/201461/5 "2011-01-08T02:18:26Z")

</div>

Hello!

On Fri, Jan 07, 2011 at 01:27:32PM -0800, Aaron Starr wrote:

> And, there is no /archives/ handler. So, here are the snippets that I think  
> try\_files $uri $uri/index.html @mongrel\_app;  
> }
> 
> Not working:
> 
> ```
> location ^~ /providers/ {
> try_files $uri /archives$uri @mongrel_app;
> }
> location / {
> try_files $uri $uri/index.html @mongrel_app;
> }
> 
> ```

This should work (and works here without problems, just tested in  
both 0.9.3 and 0.7.67). Note though that the second form (with  
try\_files) won’t work with directories.

> Also, I should mention that it in the not-working case, the behavior is that  
> it will correctly find the file in /providers/… but if the file is moved  
> to /archives/providers/…, it will no longer find it.
> 
> I’ve put all the location directives here: [http://pastie.org/1438414](http://pastie.org/1438414)

Could you please provide debug log for working and non-working  
cases? See [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/Debugging) for basic  
instructions.

Maxim D.
