# Caching for a location based on cookie

**URL:** <https://www.ruby-forum.com/t/caching-for-a-location-based-on-cookie/181652>\
**Category:** NGINX\
**Created:** [January 15, 2010, 9:08am UTC](https://www.ruby-forum.com/t/caching-for-a-location-based-on-cookie/181652 "2010-01-15T09:08:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/4bbf92/32.png) [@vys](https://www.ruby-forum.com/u/vys)\
**Post date:** [January 15, 2010, 9:08am UTC](https://www.ruby-forum.com/t/caching-for-a-location-based-on-cookie/181652/1 "2010-01-15T09:08:02Z")

</div>

Hi,  
For a given location, I want the requests to be cached only if a  
particular  
cookie is not present. How do I achieve that with nginx?  
Currently I’m trying following config

```
location /album {
    if ($cookie_logintoken ~* "") {
        set $cache_key $scheme$proxy_host$uri$is_args$args;
    }

    proxy_pass_header Set-Cookie;
    proxy_cache_key $cache_key;
    proxy_cache mem_pool;
    proxy_cache_valid 200 5m;
    proxy_pass http://$backend$uri$is_args$args;

}

```

Here, if the logintoken cookie is not present in the request, the  
cache\_key  
variable is set to a valid key string. If the cookie is present,  
cache\_key  
it will be undefined. So I expect the proxy\_cache\_key directive to not  
cache  
that request. But it seems it still caches the request with empty key.  
Is  
this the expected behavior? Is there any solution this problem?

Also, with “proxy\_cache\_valid 200 5m;” I expect only 200 responses to be  
cached. But I’m seeing 30x responses also getting cached.

_nginx version 0.7.64_

Thanks,

---

<div class="post-metadata">

**Author:** ![vys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/4bbf92/32.png) [@vys](https://www.ruby-forum.com/u/vys)\
**Post date:** [January 15, 2010, 10:08am UTC](https://www.ruby-forum.com/t/caching-for-a-location-based-on-cookie/181652/2 "2010-01-15T10:08:21Z")

</div>

On Fri, Jan 15, 2010 at 4:06 PM, Vinay Y S [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,  
> For a given location, I want the requests to be cached only if a particular cookie is not present. How do I achieve that with nginx?  
> Currently I’m trying following config  
> location /album {  
> if ($cookie\_logintoken ~\* “”) {  
> set $cache\_key $scheme$proxy\_host$uri$is\_args$args;  
> }

Please see this thread for a simple solution:

```
http://forum.nginx.org/read.php?2,25197

```

Hope this helps.

Cheers,  
-agentzh

---

<div class="post-metadata">

**Author:** ![vys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/4bbf92/32.png) [@vys](https://www.ruby-forum.com/u/vys)\
**Post date:** [January 17, 2010, 5:39pm UTC](https://www.ruby-forum.com/t/caching-for-a-location-based-on-cookie/181652/3 "2010-01-17T17:39:01Z")

</div>

On Fri, Jan 15, 2010 at 2:37 PM, agentzh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Please see this thread for a simple solution:
> 
> [http://forum.nginx.org/read.php?2,25197](http://forum.nginx.org/read.php?2,25197)
> 
> Hope this helps.

Could you please elaborate on it? There was no issues with $cache\_key  
value  
being set in if block and being visible to proxy\_cache\_key directive.  
The  
issue is when the if condition is not satisfied and hence the $cache\_key  
is  
not set, I was expecting the caching to get disabled altogether as  
proxy\_cache\_key was now being passed a uninitialized variable as  
parameter.

Thanks,  
Vinay
