A try_files problem
Igor Sysoev
is at rambler-co.ru
Thu Mar 5 23:11:58 MSK 2009
On Thu, Mar 05, 2009 at 08:52:42PM +0100, Micha?? Jaszczyk wrote:
> 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/;
> # more backends
> }
>
> location /static {
> root /some/path;
> # some other stuff
> }
>
> location /dynamic {
> rewrite ^/dynamic/(.*)$ /$1 break;
> proxy_pass http://backend;
> }
This can be handled by simple:
location /dynamic/ {
proxy_pass 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;
> proxy_store $uri;
> }
location /dynamic/ {
alias /path/to/;
try_files $uri /backend/$uri;
}
location /backend/dynamic/ {
proxy_pass http://backend/;
alias /path/to/;
proxy_store on;
}
> 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;
> 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??
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/;
proxy_store $proxy_store_path;
}
--
Igor Sysoev
http://sysoev.ru/en/
More information about the nginx
mailing list