# How to determine when the response from the cache

**URL:** <https://www.ruby-forum.com/t/how-to-determine-when-the-response-from-the-cache/219702>\
**Category:** NGINX\
**Created:** [May 12, 2012, 1:04pm UTC](https://www.ruby-forum.com/t/how-to-determine-when-the-response-from-the-cache/219702 "2012-05-12T13:04:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gurdipe\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b77776/32.png) [@Gurdipe\_D](https://www.ruby-forum.com/u/Gurdipe_D)\
**Post date:** [May 12, 2012, 1:04pm UTC](https://www.ruby-forum.com/t/how-to-determine-when-the-response-from-the-cache/219702/1 "2012-05-12T13:04:21Z")

</div>

Hello!

Can you please tell how to cope with this task.  
When nginx gives directly the page, add the title “backend\_id”.  
And when it taking from the cache, don’t add the header.

The scheme works like this:  
Frontend - nginx with proxy\_cache, followed by multiple backends  
(proxy\_pass to multiple servers).  
I need to distinguish, when a response from the cache and when it from  
the backend.  
If the response from the backend, i need to understand from which  
backend he came.  
Backends configured to add a response header backend\_id.  
But the frontend caches the entire response, with headers.  
As a result, you can understand which backend was generated response.  
But i can’t see, is it direct response, or from the cache.

Are there ways to cope with this task?  
Need your wise advice.

Posted at Nginx Forum:

> **[How to determine when the response from the cache](https://forum.nginx.org/read.php?2,226331,226331#msg-226331)**
>
> Hello! Can you please tell how to cope with this task. When nginx gives directly the page, add the title "backend\_id". And when it taking from the cache, don't add the header. The scheme works like this: Frontend - nginx with proxy\_cache,

---

<div class="post-metadata">

**Author:** ![xore](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/b782af/32.png) [@xore](https://www.ruby-forum.com/u/xore)\
**Post date:** [May 12, 2012, 1:20pm UTC](https://www.ruby-forum.com/t/how-to-determine-when-the-response-from-the-cache/219702/2 "2012-05-12T13:20:30Z")

</div>

Hi,

you can set 3 headers in this case (I’m doing the same for my front end  
server).

Those two to understand which backend server served the request and  
which HTTP status it responded with.  
add\_header X-AppServer $upstream\_addr; # Backend Server / Port  
add\_header X-AppServer-Status $upstream\_status; # Backend HTTP Status

To understand if the request was served from the Proxy Cache you can use  
add\_header X-Cache $upstream\_cache\_status; # HIT / MISS / BYPASS /  
EXPIRED

The header ‘X-Cache’ will only be sent in case it does have a value. So  
you won’t see that in case the response was not served by the proxy  
cache. A ‘Age’ header to determine how old the object is isn’t  
implemented currently (I opened an Issue re/ that a while ago).

In addition to the Headers you can easily log all Requests to your Proxy  
to another `access_log` which will contain all those information for  
central monitoring. That could look like the one here:

> <https://gist.github.com/hexa2k9/2665907>

Regards,  
Chris

Am 12.05.2012 um 13:03 schrieb xore:

---

<div class="post-metadata">

**Author:** ![xore](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/b782af/32.png) [@xore](https://www.ruby-forum.com/u/xore)\
**Post date:** [May 12, 2012, 3:56pm UTC](https://www.ruby-forum.com/t/how-to-determine-when-the-response-from-the-cache/219702/3 "2012-05-12T15:56:33Z")

</div>

Chris, thank you, this is what i need.

By the way, i found one strange thing - operator “if” don’t work with  
$upstream\_cache\_status.  
For example, i try to change “HIT” and “MISS” to more enigmatic values

set $tocache X;  
if ($upstream\_cache\_status = HIT)  
{  
set $tocache H;  
}

if ($upstream\_cache\_status = MISS)  
{  
set $tocache M;  
}

add\_header tocache “$tocache ($upstream\_cache\_status)”;

I expected to see “tocache: M (MISS)” and “tocache: H (HIT)”.  
But i got “tocache: X (MISS)” and “tocache: X (HIT)”.

Maybe “if” works not for any variable?

Posted at Nginx Forum:

> **[Re: How to determine when the response from the cache](https://forum.nginx.org/read.php?2,226331,226344#msg-226344)**
>
> Hello! Can you please tell how to cope with this task. When nginx gives directly the page, add the title "backend\_id". And when it taking from the cache, don't add the header. The scheme works like this: Frontend - nginx with proxy\_cache,

---

<div class="post-metadata">

**Author:** ![xore](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/b782af/32.png) [@xore](https://www.ruby-forum.com/u/xore)\
**Post date:** [May 14, 2012, 9:05am UTC](https://www.ruby-forum.com/t/how-to-determine-when-the-response-from-the-cache/219702/4 "2012-05-14T09:05:23Z")

</div>

“if” (as operator of module “rewrite”) works before server gets answer  
from upstream.  
That’s why $upstream\_\* variables are empty for him.  
In my case operator “map” helps me.

Posted at Nginx Forum:

> **[Re: How to determine when the response from the cache](https://forum.nginx.org/read.php?2,226331,226384#msg-226384)**
>
> Hello! Can you please tell how to cope with this task. When nginx gives directly the page, add the title "backend\_id". And when it taking from the cache, don't add the header. The scheme works like this: Frontend - nginx with proxy\_cache,
