# Is this possible?

**URL:** <https://www.ruby-forum.com/t/is-this-possible/117785>\
**Category:** NGINX\
**Created:** [October 20, 2007, 6:28am UTC](https://www.ruby-forum.com/t/is-this-possible/117785 "2007-10-20T06:28:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ben\_Marklein](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5f9b8f/32.png) [@Ben\_Marklein](https://www.ruby-forum.com/u/Ben_Marklein)\
**Post date:** [October 20, 2007, 6:28am UTC](https://www.ruby-forum.com/t/is-this-possible/117785/1 "2007-10-20T06:28:09Z")

</div>

I’m relatively new to nginx and I’m trying to figure  
out whether it’s suitable for a specific use I have.  
I’d like to set up the following for serving a high  
volume of “badge” images in a lazy mannger. These are  
images that are customized per-user, for use on social  
network site profile pages. Can someone tell me if  
this will be possible using nginx?

1. Request for /badge/username.jpg comes into nginx.  
It proxies(?) to S3 using a standardized scheme.
2. If the image is present it’s passed back via nginx.  
If not, we get a 404. nginx then proxies to a Rails  
app that generates the image on the fly, and kicks off  
a background task to upload the image to S3 so that.

If someone could tell me whether this is possible and  
ideally provide some configuration pseudocode I would  
be very grateful. Thanks!

Ben

---

<div class="post-metadata">

**Author:** ![Ben\_Marklein](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5f9b8f/32.png) [@Ben\_Marklein](https://www.ruby-forum.com/u/Ben_Marklein)\
**Post date:** [October 20, 2007, 10:52am UTC](https://www.ruby-forum.com/t/is-this-possible/117785/2 "2007-10-20T10:52:57Z")

</div>

On Fri, Oct 19, 2007 at 09:19:21PM -0700, Ben Marklein wrote:

> 1. If the image is present it’s passed back via nginx.  
> If not, we get a 404. nginx then proxies to a Rails  
> app that generates the image on the fly, and kicks off  
> a background task to upload the image to S3 so that.
> 
> If someone could tell me whether this is possible and  
> ideally provide some configuration pseudocode I would  
> be very grateful. Thanks!

S3 - is it Amazon S3 ?

```
location /badge/ {
    proxy_pass http://...S3...

    proxy_intercept_errors on;
    error_page 404 = @rails;
}

location @rails {
    proxy_pass http://rails;
}
```

---

<div class="post-metadata">

**Author:** ![Ben\_Marklein](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5f9b8f/32.png) [@Ben\_Marklein](https://www.ruby-forum.com/u/Ben_Marklein)\
**Post date:** [October 20, 2007, 11:05am UTC](https://www.ruby-forum.com/t/is-this-possible/117785/3 "2007-10-20T11:05:14Z")

</div>

I had a similar question to using nginx with Amazon’s S3. Is it possible  
for  
nginx to look in its local cache for an image, if its not there, grab it  
from S3, and cache it before sending it to the user under my domain  
instead  
of S3’s?

Thanks.

----- Original Message -----  
From: “Igor S.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Saturday, October 20, 2007 4:40 AM  
Subject: Re: is this possible?

---

<div class="post-metadata">

**Author:** ![Ben\_Marklein](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5f9b8f/32.png) [@Ben\_Marklein](https://www.ruby-forum.com/u/Ben_Marklein)\
**Post date:** [October 20, 2007, 11:33am UTC](https://www.ruby-forum.com/t/is-this-possible/117785/4 "2007-10-20T11:33:52Z")

</div>

I don’t think it’s possible to that inside nginx natively. Maybe you  
could use embedded perl or have a backend proxy like  
rails/mongrel/merb do it on nginx’s behalf.

---

<div class="post-metadata">

**Author:** ![Ben\_Marklein](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5f9b8f/32.png) [@Ben\_Marklein](https://www.ruby-forum.com/u/Ben_Marklein)\
**Post date:** [October 20, 2007, 11:47am UTC](https://www.ruby-forum.com/t/is-this-possible/117785/5 "2007-10-20T11:47:53Z")

</div>

On Sat, Oct 20, 2007 at 04:56:43AM -0400, Adam M. wrote:

> I had a similar question to using nginx with Amazon’s S3. Is it possible  
> for nginx to look in its local cache for an image, if its not there, grab  
> it from S3, and cache it before sending it to the user under my domain  
> instead of S3’s?

Currently nginx does not really sipport caching.  
However, you may implement primitive caching using mirror on demand  
(proxy\_store). This means that you should clean expired or unused files  
by itself.

recursive\_error\_pages on;

location /badge/ {  
root /data/www;  
error\_page 404 = @S3;  
}

location @S3 {  
proxy\_pass http://…S3…

```
   proxy_store on;
   proxy_store_access user:rw group:rw all:r;
   proxy_temp_path /data/temp;
   root /data/www;

   proxy_intercept_errors on;
   error_page 404 = @rails;

```

}

location @rails {  
proxy\_pass [http://rails](http://rails);

```
   proxy_store on;
   proxy_store_access user:rw group:rw all:r;
   proxy_temp_path /data/temp;
   root /data/www;

```

}
