# Is cache being used?

**URL:** <https://www.ruby-forum.com/t/is-cache-being-used/209927>\
**Category:** NGINX\
**Created:** [July 26, 2011, 5:54pm UTC](https://www.ruby-forum.com/t/is-cache-being-used/209927 "2011-07-26T17:54:58Z")\
**Posts on this page:** 10\
**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:** [July 26, 2011, 5:54pm UTC](https://www.ruby-forum.com/t/is-cache-being-used/209927/1 "2011-07-26T17:54:58Z")

</div>

Hi all. I have a newbie question:  
How can I make sure content is delivered from nginx’s cache and not from  
backend server or web-browser cache?  
How do I check it?  
Thanks!

Posted at Nginx Forum:

> **[Is cache being used?](https://forum.nginx.org/read.php?2,212954,212954#msg-212954)**
>
> Hi all. I have a newbie question: How can I make sure content is delivered from nginx's cache and not from backend server or web-browser cache? How do I check it? Thanks!

---

<div class="post-metadata">

**Author:** ![egrueda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e480ec/32.png) [@egrueda](https://www.ruby-forum.com/u/egrueda)\
**Post date:** [July 26, 2011, 6:24pm UTC](https://www.ruby-forum.com/t/is-cache-being-used/209927/2 "2011-07-26T18:24:00Z")

</div>

The more I test, the more confused I am ☹  
I’m using httperf tool to make performance testing, and when I launch a  
10 requests test, I see 10 responses in nginx logs with code 200, but I  
also see the same 10 responses with 200 code in backend ‘real’ server.  
Why is it asking backend servers again if it already has a 200 response  
for the same request?

Please, help me understand it.

I am using these settings:

* * *

## Proxy options

proxy\_buffering on;  
proxy\_cache\_min\_uses 3;  
proxy\_cache\_valid any 10m;  
proxy\_ignore\_client\_abort off;  
proxy\_intercept\_errors on;  
proxy\_next\_upstream error timeout invalid\_header;  
proxy\_redirect off;  
proxy\_set\_header Host $host;  
proxy\_set\_header X-Real-IP $remote\_addr;  
proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;  
proxy\_connect\_timeout 60;  
proxy\_send\_timeout 60;  
proxy\_read\_timeout 60;

## Cache Paths

proxy\_cache\_path /usr/local/nginx/cache/common levels=1:2  
keys\_zone=cache:10m inactive=10m max\_size=1000M;

server {  
server\_name [example.com](http://example.com) [www.example.com](http://www.example.com);  
access\_log /var/log/nginx/example.access.log;  
location / {  
proxy\_pass [http://11.22.33.44:80](http://11.22.33.44:80);  
proxy\_cache cache;  
proxy\_cache\_valid 200 1d;  
proxy\_cache\_use\_stale error timeout invalid\_header  
updating http\_500 http\_502 http\_503 http\_504;

```
    }

```

## }

==\> /var/log/nginx/example.access.log \<== (frontend server)  
10.11.12.13 - - [26/Jul/2011:18:16:07 +0200] “GET / HTTP/1.1” 200 103124  
“-” “httperf/0.9.0”  
10.11.12.13 - - [26/Jul/2011:18:16:07 +0200] “GET / HTTP/1.1” 200 103124  
“-” “httperf/0.9.0”  
10.11.12.13 - - [26/Jul/2011:18:16:07 +0200] “GET / HTTP/1.1” 200 103124  
“-” “httperf/0.9.0”  
10.11.12.13 - - [26/Jul/2011:18:16:07 +0200] “GET / HTTP/1.1” 200 103124  
“-” “httperf/0.9.0”  
10.11.12.13 - - [26/Jul/2011:18:16:08 +0200] “GET / HTTP/1.1” 200 103124  
“-” “httperf/0.9.0”  
10.11.12.13 - - [26/Jul/2011:18:16:08 +0200] “GET / HTTP/1.1” 200 103124  
“-” “httperf/0.9.0”  
10.11.12.13 - - [26/Jul/2011:18:16:08 +0200] “GET / HTTP/1.1” 200 103124  
“-” “httperf/0.9.0”  
10.11.12.13 - - [26/Jul/2011:18:16:08 +0200] “GET / HTTP/1.1” 200 103124  
“-” “httperf/0.9.0”  
10.11.12.13 - - [26/Jul/2011:18:16:08 +0200] “GET / HTTP/1.1” 200 103124  
“-” “httperf/0.9.0”  
10.11.12.13 - - [26/Jul/2011:18:16:09 +0200] “GET / HTTP/1.1” 200 103124  
“-” “httperf/0.9.0”

==\> /var/log/httpd/example.access.log \<== (backend server)  
10.11.12.13 - - [26/Jul/2011:18:16:06 +0200] “GET / HTTP/1.0” 200 103606  
“-” “httperf/0.9.0”  
10.11.12.13 - - [26/Jul/2011:18:16:06 +0200] “GET / HTTP/1.0” 200 103606  
“-” “httperf/0.9.0”  
10.11.12.13 - - [26/Jul/2011:18:16:07 +0200] “GET / HTTP/1.0” 200 103606  
“-” “httperf/0.9.0”  
10.11.12.13 - - [26/Jul/2011:18:16:07 +0200] “GET / HTTP/1.0” 200 103606  
“-” “httperf/0.9.0”  
10.11.12.13 - - [26/Jul/2011:18:16:06 +0200] “GET / HTTP/1.0” 200 103606  
“-” “httperf/0.9.0”  
10.11.12.13 - - [26/Jul/2011:18:16:06 +0200] “GET / HTTP/1.0” 200 103606  
“-” “httperf/0.9.0”  
10.11.12.13 - - [26/Jul/2011:18:16:06 +0200] “GET / HTTP/1.0” 200 103606  
“-” “httperf/0.9.0”  
10.11.12.13 - - [26/Jul/2011:18:16:07 +0200] “GET / HTTP/1.0” 200 103606  
“-” “httperf/0.9.0”  
10.11.12.13 - - [26/Jul/2011:18:16:06 +0200] “GET / HTTP/1.0” 200 103606  
“-” “httperf/0.9.0”  
10.11.12.13 - - [26/Jul/2011:18:16:07 +0200] “GET / HTTP/1.0” 200 103606  
“-” “httperf/0.9.0”

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

---

<div class="post-metadata">

**Author:** ![egrueda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e480ec/32.png) [@egrueda](https://www.ruby-forum.com/u/egrueda)\
**Post date:** [July 27, 2011, 3:38pm UTC](https://www.ruby-forum.com/t/is-cache-being-used/209927/3 "2011-07-27T15:38:48Z")

</div>

Thanks, Ben. I’ve added headers and now I can clearly see all of them:  
“X-Cache-Status: MISS” ☹  
Does it mean nginx cache is not properly configured or is it a matter of  
this specific backend?  
Why did you say “and never will be” with a “MISS” status?  
Thanks again 😉

Posted at Nginx Forum:

> **[Re: Is cache being used?](https://forum.nginx.org/read.php?2,212954,213007#msg-213007)**
>
> Hi all. I have a newbie question: How can I make sure content is delivered from nginx's cache and not from backend server or web-browser cache? How do I check it? Thanks!

---

<div class="post-metadata">

**Author:** ![egrueda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e480ec/32.png) [@egrueda](https://www.ruby-forum.com/u/egrueda)\
**Post date:** [July 26, 2011, 6:27pm UTC](https://www.ruby-forum.com/t/is-cache-being-used/209927/4 "2011-07-26T18:27:06Z")

</div>

On 26 Jul 2011, at 16:54, egrueda wrote:

> Hi all. I have a newbie question:  
> How can I make sure content is delivered from nginx’s cache and not from  
> backend server or web-browser cache?  
> How do I check it?  
> Thanks!

The $upstream\_cache\_status variable is your friend here. Add the  
following to your server config:

add\_header X-Cache-Status $upstream\_cache\_status;

You’ll see http headers like:

X-Cache-Status: MISS

in your responses. MISS if it wasn’t served from Cache (and never will  
be), HIT if it was or EXPIRED if it the cache was updated with your  
request.

You can forcibly tell any intermediate caches not to cache your  
responses with stuff like:

expires -1;  
add\_header Cache-Control private;

Hope that helps.

---

<div class="post-metadata">

**Author:** ![egrueda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e480ec/32.png) [@egrueda](https://www.ruby-forum.com/u/egrueda)\
**Post date:** [July 27, 2011, 6:53pm UTC](https://www.ruby-forum.com/t/is-cache-being-used/209927/5 "2011-07-27T18:53:21Z")

</div>

On 27 Jul 2011, at 14:38, egrueda wrote:

> Thanks, Ben. I’ve added headers and now I can clearly see all of them:  
> “X-Cache-Status: MISS” ☹  
> Does it mean nginx cache is not properly configured or is it a matter of  
> this specific backend?

It sounds to me like your application is sending back headers that  
instruct Nginx not to cache the response. Can you provide full HTTP  
headers that are coming back in the response?

> Why did you say “and never will be” with a “MISS” status?

Well, that’s not strictly true I guess depending on how your caching is  
configured (I think this is affected by the proxy\_cache\_min\_uses  
directive).

---

<div class="post-metadata">

**Author:** ![egrueda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e480ec/32.png) [@egrueda](https://www.ruby-forum.com/u/egrueda)\
**Post date:** [July 28, 2011, 8:20am UTC](https://www.ruby-forum.com/t/is-cache-being-used/209927/6 "2011-07-28T08:20:29Z")

</div>

Hi, Ben, and thanks again for your answer.  
These are the complete headers in response. This is a magento website,  
but same happens for a joomla site too.  
Maybe the “Expires” header make this object uncacheable?

* * *

## HTTP/1.1 200 OK Server: nginx Date: Thu, 28 Jul 2011 06:15:29 GMT Content-Type: image/png Connection: keep-alive Keep-Alive: timeout=60 Last-Modified: Sun, 08 Nov 2009 04:37:42 GMT Etag: “72884-ed-477d4a3387180” Accept-Ranges: bytes Content-Length: 237 X-Powered-By: PleskLin Expires: Fri, 29 Jul 2011 06:15:29 GMT Cache-Control: max-age=86400 X-Cache-Status: MISS

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

---

<div class="post-metadata">

**Author:** ![egrueda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e480ec/32.png) [@egrueda](https://www.ruby-forum.com/u/egrueda)\
**Post date:** [July 28, 2011, 9:34am UTC](https://www.ruby-forum.com/t/is-cache-being-used/209927/7 "2011-07-28T09:34:52Z")

</div>

On 28 Jul 2011, at 07:20, egrueda [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Connection: keep-alive

As far as I know, Nginx won’t cache static files, which makes sense if  
you think about it - serving from the cache is the same as serving  
from the disk.

Besides, Linux will cache frequently accessed files in memory anyway.

---

<div class="post-metadata">

**Author:** ![egrueda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e480ec/32.png) [@egrueda](https://www.ruby-forum.com/u/egrueda)\
**Post date:** [July 28, 2011, 9:50am UTC](https://www.ruby-forum.com/t/is-cache-being-used/209927/8 "2011-07-28T09:50:22Z")

</div>

On Thu, Jul 28, 2011 at 02:19:30AM -0400, egrueda wrote:

> ## Connection: keep-alive Keep-Alive: timeout=60 Last-Modified: Sun, 08 Nov 2009 04:37:42 GMT Etag: “72884-ed-477d4a3387180” Accept-Ranges: bytes Content-Length: 237 X-Powered-By: PleskLin Expires: Fri, 29 Jul 2011 06:15:29 GMT Cache-Control: max-age=86400 X-Cache-Status: MISS

What nginx version do you use ?  
Do you have any “expires” directives in nginx configuration ?

–  
Igor S.

---

<div class="post-metadata">

**Author:** ![egrueda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e480ec/32.png) [@egrueda](https://www.ruby-forum.com/u/egrueda)\
**Post date:** [October 21, 2011, 10:56am UTC](https://www.ruby-forum.com/t/is-cache-being-used/209927/9 "2011-10-21T10:56:27Z")

</div>

Same thing happening with me but I don’t have proxy\_cache\_min\_uses set  
at all so default 1 should work

Posted at Nginx Forum:

> **[Re: Is cache being used?](https://forum.nginx.org/read.php?2,212954,216992#msg-216992)**
>
> Hi all. I have a newbie question: How can I make sure content is delivered from nginx's cache and not from backend server or web-browser cache? How do I check it? Thanks!

---

<div class="post-metadata">

**Author:** ![egrueda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e480ec/32.png) [@egrueda](https://www.ruby-forum.com/u/egrueda)\
**Post date:** [July 28, 2011, 10:10am UTC](https://www.ruby-forum.com/t/is-cache-being-used/209927/10 "2011-07-28T10:10:12Z")

</div>

Thanks, Ben, but in this case I need to server even static objects from  
frontend servers, not backend.

Hi, Igor. Version is nginx/1.0.5  
In my common config file I see “expires 24h;” I see no other expires  
directive.

But wait!  
I’ve commented out “proxy\_cache\_min\_uses” parameters, so it defaults to  
1  
Now, first request returns a MISS header, but next request DOES returns  
a “X-Cache-Status: HIT”

Maybe my “problem” was with the proxy\_cache\_min\_uses parameter, as I  
wasn’t making the same request three times?  
Humm… not sure, I’ve refreshed same page about hundreds of times 🙂  
Now I’m tailing -f both logs and I see all files are server by proxy  
without taking them from the original backend server

Thanks both for your answers, and sorry for this newbie question :-S

Posted at Nginx Forum:

> **[Re: Is cache being used?](https://forum.nginx.org/read.php?2,212954,213032#msg-213032)**
>
> Hi all. I have a newbie question: How can I make sure content is delivered from nginx's cache and not from backend server or web-browser cache? How do I check it? Thanks!
