# A try\_files problem

**URL:** <https://www.ruby-forum.com/t/a-try-files-problem/162112>\
**Category:** NGINX\
**Created:** [March 5, 2009, 9:02pm UTC](https://www.ruby-forum.com/t/a-try-files-problem/162112 "2009-03-05T21:02:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MichaSSSSSJaszczyk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a3d4f5/32.png) [@MichaSSSSSJaszczyk](https://www.ruby-forum.com/u/MichaSSSSSJaszczyk)\
**Post date:** [March 5, 2009, 9:02pm UTC](https://www.ruby-forum.com/t/a-try-files-problem/162112/1 "2009-03-05T21:02:00Z")

</div>

Hi,

I’m having some hard time with try\_files and rewrite directives. I  
already spent few hours on this and my brain is exploding :). Can  
anyone help me?

My situation is as follows: I use Nginx to serve static files and  
proxy dynamic requests to Python backends. That’s simple and my config  
file looks like this:

upstream backend {  
[http://backend1/](http://backend1/);  
# more backends  
}

location /static {  
root /some/path;  
# some other stuff  
}

location /dynamic {  
rewrite ^/dynamic/(.\*)$ /$1 break;  
proxy\_pass [http://backend](http://backend);  
}

My dynamic content has this property: It doesn’t change over time. So  
each time I send the same request to the backends, the response will  
always be the same. So I can mirror it using proxy\_store:

location /dynamic {  
rewrite ^/dynamic/(.\*)$ /$1 break;  
try\_files $uri @backend;  
}

location @backend {  
proxy\_pass [http://backend](http://backend);  
proxy\_store $uri;  
}

This works just great. But there’s one gotcha: Some of the requests to  
the backend have the following form: /special\_request/ID. This ID has  
many potential values and if I mirror them the way described above, I  
will end up having a directory with tens (or hundreds) of thousands of  
files which will kill the file system eventually. So I did the  
following:

location @backend {  
set $proxy\_store\_path $uri;  
if ($uri ~ “^/special\_request/([0-9]{2})([0-9]{2})(.\*)$”) {  
set $proxy\_store\_path /$1/$2/$3;  
}  
proxy\_pass [http://backend](http://backend);  
proxy\_store $proxy\_store\_path;  
}

This also works nice - instead of a flat directory with tons of files  
I have a nice 2-level hierarchy of directories. But… I also need to  
apply this idea to the /dynamic location. The most obvious approach  
is:

location /dynamic {  
rewrite ^/dynamic/(._)$ /$1 break;  
set $try\_files\_path $uri;  
if ($uri ~ "^/special\_request/([0-9]{2})([0-9]{2})(._)$") {  
set $try\_files\_path /$1/$2/$3;  
}  
try\_files $try\_files\_path @backend;  
}

But this doesn’t work… I spent many hours trying to figure out how  
to deal with this, but no success so far… What’s more interesting,  
in the log file I see a warning that $try\_files\_path is not  
initialized! How come??

Please help, this problem is melting my brain :).

Regards,

---

<div class="post-metadata">

**Author:** ![MichaSSSSSJaszczyk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a3d4f5/32.png) [@MichaSSSSSJaszczyk](https://www.ruby-forum.com/u/MichaSSSSSJaszczyk)\
**Post date:** [March 5, 2009, 9:23pm UTC](https://www.ruby-forum.com/t/a-try-files-problem/162112/2 "2009-03-05T21:23:47Z")

</div>

On Thu, Mar 05, 2009 at 08:52:42PM +0100, Micha?? Jaszczyk wrote:

> ```
> # more backends
> 
> ```
> 
> }  
> This can be handled by simple:

location /dynamic/ {  
proxy\_pass [http://backend/](http://backend/);  
}

> ```
> proxy_pass http://backend;
> proxy_store $uri;
> 
> ```
> 
> }

location /dynamic/ {  
alias /path/to/;  
try\_files $uri /backend/$uri;  
}

location /backend/dynamic/ {  
proxy\_pass [http://backend/](http://backend/);

```
alias /path/to/;
proxy_store on;

```

}

> ```
> set $proxy_store_path /$1/$2/$3;
> 
> ```
> 
> location /dynamic {  
> in the log file I see a warning that $try\_files\_path is not  
> initialized! How come??

You may try a patch in email with subject “captures in regex locations”:

location ~ “^/dynamic/special\_request/([0-9]{2})([0-9]{2})(.\*)$” {  
set $proxy\_store\_path /$1/$2/$3;

```
try_files $proxy_store_path /backend/$uri;

```

}

location /backend/dynamic/ {  
proxy\_pass [http://backend/](http://backend/);

```
proxy_store $proxy_store_path;

```

}

---

<div class="post-metadata">

**Author:** ![MichaSSSSSJaszczyk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a3d4f5/32.png) [@MichaSSSSSJaszczyk](https://www.ruby-forum.com/u/MichaSSSSSJaszczyk)\
**Post date:** [March 5, 2009, 11:24pm UTC](https://www.ruby-forum.com/t/a-try-files-problem/162112/3 "2009-03-05T23:24:26Z")

</div>

> You may try a patch in email with subject “captures in regex locations”:
> 
> location ~ “^/dynamic/special\_request/([0-9]{2})([0-9]{2})(.\*)$” {  
> Â Â set $proxy\_store\_path /$1/$2/$3;
> 
> Â Â try\_files Â $proxy\_store\_path Â /backend/$uri;  
> }

I tried the patch but it I still can’t make it working. Here’s part of  
my config file:

```
    location ~

```

“^/dynamic/special\_request/([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{44})$”  
{  
root /var/nginx/storage\_cache;  
set $mirror\_path /$1/$2/$3;  
try\_files $mirror\_path /storage$uri;  
}

And here’s part of the debug:

2009/03/05 23:11:13 [debug] 14755#0: \*1 using configuration  
“^/dynamic/special\_request/([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{44})$”  
2009/03/05 23:11:13 [debug] 14755#0: \*1 http cl:-1 max:1048576  
2009/03/05 23:11:13 [debug] 14755#0: \*1 generic phase: 3  
2009/03/05 23:11:13 [debug] 14755#0: \*1 http script complex value  
2009/03/05 23:11:13 [debug] 14755#0: \*1 http script copy: “/”  
2009/03/05 23:11:13 [debug] 14755#0: \*1 http script capture: “”  
2009/03/05 23:11:13 [debug] 14755#0: \*1 http script copy: “/”  
2009/03/05 23:11:13 [debug] 14755#0: \*1 http script capture: “”  
2009/03/05 23:11:13 [debug] 14755#0: \*1 http script copy: “/”  
2009/03/05 23:11:13 [debug] 14755#0: \*1 http script capture: “”  
2009/03/05 23:11:13 [debug] 14755#0: \*1 http script set $mirror\_path

It seems like captures are not used. Any idea?

Regards,
