# Skipping the proxy cache based on a cookie?

**URL:** <https://www.ruby-forum.com/t/skipping-the-proxy-cache-based-on-a-cookie/201066>\
**Category:** NGINX\
**Created:** [December 29, 2010, 3:16pm UTC](https://www.ruby-forum.com/t/skipping-the-proxy-cache-based-on-a-cookie/201066 "2010-12-29T15:16:54Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![ehudros2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/76d3ee/32.png) [@ehudros2](https://www.ruby-forum.com/u/ehudros2)\
**Post date:** [January 20, 2011, 9:10pm UTC](https://www.ruby-forum.com/t/skipping-the-proxy-cache-based-on-a-cookie/201066/21 "2011-01-20T21:10:48Z")

</div>

Thanks agentzh.

The Lua code you gave works and does what I need here.

Still would be great to have an option like …

```auto
proxy_cache_bypass_empty $var;
proxy_no_cache_empty $var;
```

… in the main proxy module though.

Anyway, solved at least.

Thanks again.

Posted at Nginx Forum:

> **[Re: Skipping the proxy cache based on a cookie?](https://forum.nginx.org/read.php?2,161969,168015#msg-168015)**
>
> Hi everyone, I'm looking into setting up nginx as a proxy cache. I was wondering if someone can elaborate a bit about ways to bypass the cache in certain requests. For example, I'd like every new user to go to the backend server once in order for...

---

<div class="post-metadata">

**Author:** ![ehudros2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/76d3ee/32.png) [@ehudros2](https://www.ruby-forum.com/u/ehudros2)\
**Post date:** [January 20, 2011, 9:35pm UTC](https://www.ruby-forum.com/t/skipping-the-proxy-cache-based-on-a-cookie/201066/22 "2011-01-20T21:35:58Z")

</div>

On 20 Jan 2011 04h05 WET, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > if ($cookie\_COOKIE = “”) {  
> > set $cookie\_no\_cache true;  
> > }
> > 
> > Now it works as “usual”.
> 
> Nginx’s “if” is evil and can be even more evil when you use it a bit  
> more. The code snippet above can only work when being put into the  
> “server” block rather than the “location” block 🙂

Ok. Care to elaborate on that? I think is something that should be  
explicitly stated in the IfIsEvil wiki page.

Thanks,  
— appa

---

<div class="post-metadata">

**Author:** ![ehudros2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/76d3ee/32.png) [@ehudros2](https://www.ruby-forum.com/u/ehudros2)\
**Post date:** [January 21, 2011, 9:52pm UTC](https://www.ruby-forum.com/t/skipping-the-proxy-cache-based-on-a-cookie/201066/23 "2011-01-21T21:52:59Z")

</div>

Seems as if I may have spoken too soon on the Lua option.

Now, it does check for the existence of the cookie and set the variable  
accordingly, but I am almost drawing the conclusion that proxy\_no\_cache  
and proxy\_cache\_bypass do not accept user defined variables as input as  
whether the extra content from the backend in my case seems to be a hit  
or miss affair and it seems the only time I can guarantee that the  
content is served from the back end is when I either purge the cache or  
run those two against a cookie.

Would appreciate some clarification.

Thanks

Posted at Nginx Forum:

> **[Re: Skipping the proxy cache based on a cookie?](https://forum.nginx.org/read.php?2,161969,168389#msg-168389)**
>
> Hi everyone, I'm looking into setting up nginx as a proxy cache. I was wondering if someone can elaborate a bit about ways to bypass the cache in certain requests. For example, I'd like every new user to go to the backend server once in order for...

---

<div class="post-metadata">

**Author:** ![ehudros2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/76d3ee/32.png) [@ehudros2](https://www.ruby-forum.com/u/ehudros2)\
**Post date:** [January 21, 2011, 9:53pm UTC](https://www.ruby-forum.com/t/skipping-the-proxy-cache-based-on-a-cookie/201066/24 "2011-01-21T21:53:37Z")

</div>

PS.

Makes a “proxy\_cache\_bypass\_empty” option more and more appealing

Posted at Nginx Forum:

> **[Re: Skipping the proxy cache based on a cookie?](https://forum.nginx.org/read.php?2,161969,168390#msg-168390)**
>
> Hi everyone, I'm looking into setting up nginx as a proxy cache. I was wondering if someone can elaborate a bit about ways to bypass the cache in certain requests. For example, I'd like every new user to go to the backend server once in order for...

---

<div class="post-metadata">

**Author:** ![ehudros2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/76d3ee/32.png) [@ehudros2](https://www.ruby-forum.com/u/ehudros2)\
**Post date:** [January 20, 2011, 5:07am UTC](https://www.ruby-forum.com/t/skipping-the-proxy-cache-based-on-a-cookie/201066/25 "2011-01-20T05:07:12Z")

</div>

On Wed, Jan 19, 2011 at 5:52 AM, Antnio P. P. Almeida [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> set $cookie\_no\_cache true;  
> }
> 
> Now it works as “usual”.

Nginx’s “if” is evil and can be even more evil when you use it a bit  
more. The code snippet above can only work when being put into the  
“server” block rather than the “location” block 🙂

I believe this is a perfect use case for ngx\_lua:

location / {  
set\_by\_lua $no\_cache ’  
if ngx.var.cookie\_COOKIE == “” or ngx.var.cookie\_COOKIE == nil  
then  
return 1  
end  
return “”  
';  
proxy\_cache\_bypass $no\_cache;  
proxy\_pass …  
}

Actually you can implement even more complicated caching policy this  
way because Lua is a turing complete language and also has great  
performance 😉

See [GitHub - openresty/lua-nginx-module: Embed the Power of Lua into NGINX HTTP servers · GitHub](https://github.com/chaoslawful/lua-nginx-module) for more details.

Cheers,  
-agentzh

---

<div class="post-metadata">

**Author:** ![ehudros2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/76d3ee/32.png) [@ehudros2](https://www.ruby-forum.com/u/ehudros2)\
**Post date:** [January 21, 2011, 10:18pm UTC](https://www.ruby-forum.com/t/skipping-the-proxy-cache-based-on-a-cookie/201066/26 "2011-01-21T22:18:43Z")

</div>

OK finally figured it out I think.

Seems it may be to do with the scope of the user set variables (not  
sure) but after moving adding proxy\_no\_cache and proxy\_cache\_bypass to  
the same location instead of the centrally called ones, It seems to be  
working as expected.

The hint was in agentzh’s original post

location / {  
set\_by\_lua $no\_cache ’  
if ngx.var.cookie\_COOKIE == “” or ngx.var.cookie\_COOKIE == nil then  
return 1  
end  
return “”  
';  
proxy\_cache\_bypass $no\_cache;  
proxy\_pass …  
}

Posted at Nginx Forum:

> **[Re: Skipping the proxy cache based on a cookie?](https://forum.nginx.org/read.php?2,161969,168394#msg-168394)**
>
> Hi everyone, I'm looking into setting up nginx as a proxy cache. I was wondering if someone can elaborate a bit about ways to bypass the cache in certain requests. For example, I'd like every new user to go to the backend server once in order for...

[Previous page](https://www.ruby-forum.com/t/skipping-the-proxy-cache-based-on-a-cookie/201066.md?page=1)
