A try_files problem

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/;
# more backends
}

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

location /dynamic {
rewrite ^/dynamic/(.*)$ /$1 break;
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;
}

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??

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

Regards,

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/;
}

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;

}

    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/;

proxy_store  $proxy_store_path;

}

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,