A try_files problem

Michał Jaszczyk jasiu85 at gmail.com
Thu Mar 5 22:52:42 MSK 2009


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,
-- 
Michał Jaszczyk





More information about the nginx mailing list