# Proxy cache

**URL:** <https://www.ruby-forum.com/t/proxy-cache/228342>\
**Category:** NGINX\
**Created:** [April 18, 2013, 6:20pm UTC](https://www.ruby-forum.com/t/proxy-cache/228342 "2013-04-18T18:20:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 18, 2013, 6:20pm UTC](https://www.ruby-forum.com/t/proxy-cache/228342/1 "2013-04-18T18:20:00Z")

</div>

Hello,

I am using nginx as a reverse proxy to cache content for an application.  
Requests to the application are expensive, so I would like to set up  
caching so that if the file exists in nginx, it won’t even bother  
querying the backend server.

I can’t seem to figure out what I am missing.

This is how I am set up:

location /download {  
index index.html index.htm;  
proxy\_pass [http://x.x.x.x/download](http://x.x.x.x/download);  
proxy\_set\_header X-Real-IP $remote\_addr;  
proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;  
proxy\_set\_header Host [application.domain.com](http://application.domain.com);  
proxy\_set\_header Accept-Encoding “”;  
proxy\_ignore\_headers Set-Cookie X-Accel-Expires Expires Cache-Control;  
if\_modified\_since off;  
add\_header X-Cache-Status $upstream\_cache\_status;

proxy\_cache\_valid 200 24h;  
expires 168h;  
proxy\_cache staticfilecache;  
}

proxy\_cache\_path /var/www/nginxcache/ levels=1:1:2  
keys\_zone=staticfilecache:2000m inactive=10800m;  
proxy\_cache\_key “$scheme$host$request\_uri$cookie\_user”;

So for all requests to /download, I want it to serve strictly from the  
cache. I do not want it to query the proxy\_pass location at all (not  
even for last modified time) if the file exists in the local cache. I  
just want it to serve the cached copy and be done.

Is this possible?

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 18, 2013, 7:05pm UTC](https://www.ruby-forum.com/t/proxy-cache/228342/2 "2013-04-18T19:05:35Z")

</div>

Am completely sure its possible, am just not the nginx specialist, so i  
might not point you to the best directions.

But as far as i understand in my little time together with things  
beautiful  
thing called nginx, you should have a look on this:

[http://wiki.nginx.org/HttpCoreModule#root](http://wiki.nginx.org/HttpCoreModule#root)

let me know the results you achieve, since i’ll be soon passing by the  
same  
problem 😛

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 24, 2013, 12:04pm UTC](https://www.ruby-forum.com/t/proxy-cache/228342/3 "2013-04-24T12:04:22Z")

</div>

> Is this possible?

yes. depending on your setup it could worth the  
try to use nginx as static server for your download-files,  
esp. if you run your proxy\_pass - location on on the  
same server. os-cache can be as fast as a ram-cache  
via tmpfs.

depending on the amount of files in /download and filesize  
it can be useful to tweak your setup (buffers, sendfile etc)

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,238485,238595#msg-238595](http://forum.nginx.org/read.php?2,238485,238595#msg-238595)
