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

Hi all,

I am using nginx’s Proxy module to retrieve images from Amazon S3. I
need to
be able to tell the Proxy module to retrieve a smaller sized file if a
bigger sized file doesnt exist. How can I do that?

For instance, my nginx configuration:

location / {
proxy_pass https://s3.amazonaws.com/my_bucket/;
error_page 415 = /empty;
}

Say I have image1_100.jpg on S3 then http://localhost/image1_100.jpg
works
fine. However http://localhost/image1_300.jpg wont work. I want to be
able
to ask nginx to request the
https://s3.amazonaws.com/my_bucket/image1_300.jpg file and if it doesnt
exist, request for iamge1_100.jpg file instead.

Thanks!

Posted at Nginx Forum:

Hello!

On Wed, Oct 03, 2012 at 05:00:39PM -0400, wurb32 wrote:

        error_page   415 = /empty;

}

Say I have image1_100.jpg on S3 then http://localhost/image1_100.jpg works
fine. However http://localhost/image1_300.jpg wont work. I want to be able
to ask nginx to request the
https://s3.amazonaws.com/my_bucket/image1_300.jpg file and if it doesnt
exist, request for iamge1_100.jpg file instead.

Use error_page 404 and proxy_intercept_errors, see
Module ngx_http_proxy_module.


Maxim D.

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?

Posted at Nginx Forum:

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 D.