How to tell Proxy module to retrieve secondary URL if primary URL doenst exist

Maxim Dounin mdounin at mdounin.ru
Fri Oct 5 12:33:20 UTC 2012


Hello!

On Thu, Oct 04, 2012 at 12:08:55PM -0400, wurb32 wrote:

> Hi ,
> 
> I tried with error_page and proxy_intercept_errors and it is not working for
> me. Do you mind if you can give me an example configuration?

Trivial config with fallback to a predefined static file would 
look like:

    location / {
        error_page 404 = /fallback.jpg;

        proxy_pass http://upstream;
        proxy_intercept_errors on;
    }

    location = /fallback.jpg {
        # serve static file
    }

If you want rewrite from /something_300.jpg to /something_100.jpg 
to happen automatically, for all possible values of "something", 
then config like this should work, using named location and 
rewrite:

    location / {
        error_page 404 = @fallback;

        proxy_pass http://upstream;
        proxy_intercept_errors on;
    }

    location @fallback {

        # if request ends with _300.jpg - rewrite to _100.jpg
        # and stop processing of rewrite rules, i.e. continue
        # with proxy_pass; else return 404

        rewrite ^(.*)_300.jpg$ $1_100.jpg break;
        return 404;

        proxy_pass http://upstream;
    }

Documentation:

http://nginx.org/r/proxy_intercept_errors
http://nginx.org/r/error_page
http://nginx.org/r/location
http://nginx.org/r/rewrite

-- 
Maxim Dounin
http://nginx.com/support.html



More information about the nginx mailing list