# Can I cache a page conditionally based on cookie value?

**URL:** <https://www.ruby-forum.com/t/can-i-cache-a-page-conditionally-based-on-cookie-value/228989>\
**Category:** NGINX\
**Created:** [May 15, 2013, 3:23am UTC](https://www.ruby-forum.com/t/can-i-cache-a-page-conditionally-based-on-cookie-value/228989 "2013-05-15T03:23:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![patng323](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/90ced4/32.png) [@patng323](https://www.ruby-forum.com/u/patng323)\
**Post date:** [May 15, 2013, 3:23am UTC](https://www.ruby-forum.com/t/can-i-cache-a-page-conditionally-based-on-cookie-value/228989/1 "2013-05-15T03:23:25Z")

</div>

Hi,

Using NginX as a reverse proxy, I want to cache the response ONLY if the  
user has NOT logged in, which can be checked by testing the existence of  
a  
cookie.

First I tried to use “if”, but NginX complained that I cannot put a  
proxy\_cache\_valid inside an “if”:

location / {  
proxy\_pass [http://www.xyz.com](http://www.xyz.com);  
if ($cookie\_login = ‘’) {  
proxy\_cache my-cache;  
proxy\_cache\_valid 200 301 302 60m;  
}  
}

Then I tried to set the cache-duration to 0s by using a variable, but  
then  
NginX complained the time value is incorrect.  
set $cacheDuration 60m;  
if ($cookie\_login != ‘’) {  
set $cacheDuration 0s;  
}

proxy\_cache my-cache;  
proxy\_cache\_valid 200 301 302 $cacheDuration

So is there any way to cache conditionally based on the existence of a  
cookie?

Thanks.

- Patrick

Posted at Nginx Forum:

> **[Can I cache a page conditionally based on cookie value?](https://forum.nginx.org/read.php?2,239207,239207#msg-239207)**
>
> Hi, Using NginX as a reverse proxy, I want to cache the response ONLY if the user has NOT logged in, which can be checked by testing the existence of a cookie. First I tried to use "if", but NginX complained that I cannot put a...

---

<div class="post-metadata">

**Author:** ![patng323](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/90ced4/32.png) [@patng323](https://www.ruby-forum.com/u/patng323)\
**Post date:** [May 15, 2013, 12:16pm UTC](https://www.ruby-forum.com/t/can-i-cache-a-page-conditionally-based-on-cookie-value/228989/2 "2013-05-15T12:16:14Z")

</div>

Maxim, that’s what I need. Thanks a lot!

Posted at Nginx Forum:

> **[Re: Can I cache a page conditionally based on cookie value?](https://forum.nginx.org/read.php?2,239207,239220#msg-239220)**
>
> Hi, Using NginX as a reverse proxy, I want to cache the response ONLY if the user has NOT logged in, which can be checked by testing the existence of a cookie. First I tried to use "if", but NginX complained that I cannot put a...

---

<div class="post-metadata">

**Author:** ![patng323](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/90ced4/32.png) [@patng323](https://www.ruby-forum.com/u/patng323)\
**Post date:** [May 15, 2013, 11:18am UTC](https://www.ruby-forum.com/t/can-i-cache-a-page-conditionally-based-on-cookie-value/228989/3 "2013-05-15T11:18:59Z")

</div>

Hello!

On Tue, May 14, 2013 at 09:23:08PM -0400, patng323 wrote:

> proxy\_pass [http://www.xyz.com](http://www.xyz.com);  
> if ($cookie\_login != ‘’) {  
> set $cacheDuration 0s;  
> }
> 
> proxy\_cache my-cache;  
> proxy\_cache\_valid 200 301 302 $cacheDuration
> 
> So is there any way to cache conditionally based on the existence of a  
> cookie?

Something like

```
proxy_no_cache $cookie_login;

```

should do the trick. See [Module ngx\_http\_proxy\_module](http://nginx.org/r/proxy_no_cache) for  
details.

–  
Maxim D.

> **[nginx: donation](http://nginx.org/en/donation.html)**
