# Proxy caching

**URL:** <https://www.ruby-forum.com/t/proxy-caching/206690>\
**Category:** NGINX\
**Created:** [May 7, 2011, 2:50pm UTC](https://www.ruby-forum.com/t/proxy-caching/206690 "2011-05-07T14:50:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [May 7, 2011, 2:50pm UTC](https://www.ruby-forum.com/t/proxy-caching/206690/1 "2011-05-07T14:50:17Z")

</div>

Hi all,

I would like to use nginx to cache some responses from my backends.  
There are basically 2 types of responses I would like to cache.

- public images that never expire
- private content

The first one was easy, got that working already. Since my application  
uses timestamps in the html when referring to images, changes to them  
will be instantly picked up.

The second one is way harder.  
Responses can become quite large (1 upto 10Mb).  
My backend application is putting etags on the responses so it can  
return 304s if nothing changed.  
Also, it puts a 5min max-age cache-control on it,  
Lastly, responses are marked private, because they are behind http basic  
authentication (handled by the backend app).

What I would like nginx to do is the following:  
It should store the first response for a certain url, using the current  
http auth in the key (so it won’t accidentally become publicly  
available).  
If a second request comes in for the same url with the same http auth  
within 5 minutes: serve from cache.  
If a second request comes in for the same url with the same http auth  
_after_ 5 mintutes: ask backend using the if-not-modified header.

- if modified, store the new response.
- if 304 not modified, respond from cache, update cache to be valid for  
5 minutes again.  
If a request comes in for the same url but with other http auth: ignore  
cache and ask backend.

The http auth stuff seems solvable by using the http auth in the  
cache\_key.  
The other 2 things I would like to solve are:

- I can’t find clear info about nginx’s handling of etag and  
if-not-modified. Will it work as described above out-of-the-box?  
It’s especially important that nginx uses if-not-modified when querying  
the backend, even if the client request itself didn’t contain it.  
Ofcourse if the client request did include it, it should receive a nice  
304 too.

- The backend sends Cache-Control: private for these responses. I would  
like nginx to cache them though.  
Ignoring the Cache-Control header doesn’t seem to work (because without  
cache-control at all, caching seems disabled)  
Another option might be to have the backend set cache-control to public  
and have nginx set it back to private, but that sounds a bit hacky.

Does anyone have any suggestions about how to proceed?  
Thanks a lot!

Mathijs

Posted at Nginx Forum:

> **[proxy caching](https://forum.nginx.org/read.php?2,196280,196280#msg-196280)**
>
> Hi all, I would like to use nginx to cache some responses from my backends. There are basically 2 types of responses I would like to cache. - public images that never expire - private content The first one was easy, got that working already.

---

<div class="post-metadata">

**Author:** ![bluescreen303](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/958977/32.png) [@bluescreen303](https://www.ruby-forum.com/u/bluescreen303)\
**Post date:** [May 8, 2011, 1:29am UTC](https://www.ruby-forum.com/t/proxy-caching/206690/2 "2011-05-08T01:29:55Z")

</div>

Hello!

On Sat, May 07, 2011 at 08:49:43AM -0400, bluescreen303 wrote:

> will be instantly picked up.  
> It should store the first response for a certain url, using the current  
> cache and ask backend.  
> 304 too.  
> Right now nginx can’t use conditional requests to refresh cache.

> - The backend sends Cache-Control: private for these responses. I would  
> like nginx to cache them though.  
> Ignoring the Cache-Control header doesn’t seem to work (because without  
> cache-control at all, caching seems disabled)  
> Another option might be to have the backend set cache-control to public  
> and have nginx set it back to private, but that sounds a bit hacky.

Solution is to ignore Cache-Control header and enable caching  
explicitly with proxy\_cache\_valid directive.

Maxim D.

---

<div class="post-metadata">

**Author:** ![bluescreen303](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/958977/32.png) [@bluescreen303](https://www.ruby-forum.com/u/bluescreen303)\
**Post date:** [May 8, 2011, 6:17pm UTC](https://www.ruby-forum.com/t/proxy-caching/206690/3 "2011-05-08T18:17:59Z")

</div>

Hi,

give varnish [1] a look, its probably better suited for such a scenario.

[1] [http://www.varnish-cache.org/](http://www.varnish-cache.org/)
