Is cache being used?

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:

The more I test, the more confused I am :frowning:
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 www.example.com;
access_log /var/log/nginx/example.access.log;
location / {
proxy_pass 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:

Thanks, Ben. I’ve added headers and now I can clearly see all of them:
“X-Cache-Status: MISS” :frowning:
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 :wink:

Posted at Nginx Forum:

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.

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” :frowning:
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).

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:

On 28 Jul 2011, at 07:20, egrueda [email protected] 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.

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.

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:

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 :slight_smile:
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: