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

**URL:** <https://www.ruby-forum.com/t/how-to-tell-proxy-module-to-retrieve-secondary-url-if-primary-url-doenst-exist/223431>\
**Category:** NGINX\
**Created:** [October 3, 2012, 11:01pm UTC](https://www.ruby-forum.com/t/how-to-tell-proxy-module-to-retrieve-secondary-url-if-primary-url-doenst-exist/223431 "2012-10-03T23:01:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [October 3, 2012, 11:01pm UTC](https://www.ruby-forum.com/t/how-to-tell-proxy-module-to-retrieve-secondary-url-if-primary-url-doenst-exist/223431/1 "2012-10-03T23:01:03Z")

</div>

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/](https://s3.amazonaws.com/my_bucket/);  
error\_page 415 = /empty;  
}

Say I have image1\_100.jpg on S3 then [http://localhost/image1\_100.jpg](http://localhost/image1_100.jpg)  
works  
fine. However [http://localhost/image1\_300.jpg](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](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:

> **[How to tell Proxy module to retrieve secondary URL if primary URL doenst exist](https://forum.nginx.org/read.php?2,231364,231364#msg-231364)**
>
> 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:...

---

<div class="post-metadata">

**Author:** ![wurb32](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ed8c4c/32.png) [@wurb32](https://www.ruby-forum.com/u/wurb32)\
**Post date:** [October 4, 2012, 11:46am UTC](https://www.ruby-forum.com/t/how-to-tell-proxy-module-to-retrieve-secondary-url-if-primary-url-doenst-exist/223431/2 "2012-10-04T11:46:22Z")

</div>

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](http://localhost/image1_100.jpg) works  
> fine. However [http://localhost/image1\_300.jpg](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](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](http://nginx.org/r/proxy_intercept_errors).

–  
Maxim D.

> **[F5 NGINX: Built for cloud-native apps. Ready for AI traffic.](https://www.f5.com/products/nginx)**
>
> Deliver modern applications with NGINX. Explore high-performance load balancing, API gateways, and web server solutions trusted by top global enterprises.

---

<div class="post-metadata">

**Author:** ![wurb32](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ed8c4c/32.png) [@wurb32](https://www.ruby-forum.com/u/wurb32)\
**Post date:** [October 4, 2012, 6:09pm UTC](https://www.ruby-forum.com/t/how-to-tell-proxy-module-to-retrieve-secondary-url-if-primary-url-doenst-exist/223431/3 "2012-10-04T18:09:43Z")

</div>

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:

> **[Re: How to tell Proxy module to retrieve secondary URL if primary URL doenst...](https://forum.nginx.org/read.php?2,231364,231389#msg-231389)**
>
> 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:...

---

<div class="post-metadata">

**Author:** ![wurb32](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ed8c4c/32.png) [@wurb32](https://www.ruby-forum.com/u/wurb32)\
**Post date:** [October 5, 2012, 2:34pm UTC](https://www.ruby-forum.com/t/how-to-tell-proxy-module-to-retrieve-secondary-url-if-primary-url-doenst-exist/223431/4 "2012-10-05T14:34:16Z")

</div>

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:

> **[Module ngx\_http\_proxy\_module](http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_intercept_errors)**
>
> The ngx\_http\_proxy\_module module allows passing requests to another server.

> **[Module ngx\_http\_core\_module](http://nginx.org/en/docs/http/ngx_http_core_module.html#error_page)**
>
> This directive appeared in version 1.11.8.

> **[Module ngx\_http\_core\_module](http://nginx.org/en/docs/http/ngx_http_core_module.html#location)**
>
> This directive appeared in version 1.11.8.

> **[Module ngx\_http\_rewrite\_module](http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite)**
>
> The ngx\_http\_rewrite\_module module is used to change request URI using PCRE regular expressions, return redirects, and conditionally select configurations.

–  
Maxim D.

> **[F5 NGINX: Built for cloud-native apps. Ready for AI traffic.](https://www.f5.com/products/nginx)**
>
> Deliver modern applications with NGINX. Explore high-performance load balancing, API gateways, and web server solutions trusted by top global enterprises.
