# Nginx logic (if cookie set, redirect hereâ€¦) Is it possible?

**URL:** <https://www.ruby-forum.com/t/nginx-logic-if-cookie-set-redirect-herea-is-it-possible/189317>\
**Category:** NGINX\
**Created:** [May 28, 2010, 4:47pm UTC](https://www.ruby-forum.com/t/nginx-logic-if-cookie-set-redirect-herea-is-it-possible/189317 "2010-05-28T16:47:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![thomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e36b37/32.png) [@thomas](https://www.ruby-forum.com/u/thomas)\
**Post date:** [May 28, 2010, 4:47pm UTC](https://www.ruby-forum.com/t/nginx-logic-if-cookie-set-redirect-herea-is-it-possible/189317/1 "2010-05-28T16:47:47Z")

</div>

So, I have a pretty basic need, but I can’t figure out if it’s even  
possible, much less how to do it.

I have a main page that anyone can see. Most of the rest of the  
application can be seen only if logged in (hence, a “set cookie”). So I  
was thinking, as long as they don’t have a cookie set, they can just see  
a cached version of nginx. I can get it caching with this:

proxy\_cache STATIC;  
proxy\_cache\_valid 200 1d;  
proxy\_cache\_use\_stale error timeout invalid\_header updating  
http\_500 http\_502 http\_503 http\_504;

And it helps a ton. (instead of 15 requests per second it gets over  
1000). Now I just need some sort of “server logic” to say only serve the  
cached page if they have no cookie, otherwise, load the dynamic page  
(which will automatically redirect them into the app).

Any ideas?

---

<div class="post-metadata">

**Author:** ![thomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e36b37/32.png) [@thomas](https://www.ruby-forum.com/u/thomas)\
**Post date:** [May 28, 2010, 4:52pm UTC](https://www.ruby-forum.com/t/nginx-logic-if-cookie-set-redirect-herea-is-it-possible/189317/2 "2010-05-28T16:52:27Z")

</div>

On Fri, May 28, 2010 at 04:47:47PM +0200, Thomas Delonge wrote:

> proxy\_cache\_use\_stale error timeout invalid\_header updating  
> http\_500 http\_502 http\_503 http\_504;
> 
> And it helps a ton. (instead of 15 requests per second it gets over  
> 1000). Now I just need some sort of “server logic” to say only serve the  
> cached page if they have no cookie, otherwise, load the dynamic page  
> (which will automatically redirect them into the app).
> 
> Any ideas?

You should get the latest nginx version 0.8.38 and add:

```
proxy_no_cache $cookie_NAME;

```

–  
Igor S.  
[http://sysoev.ru/en/](http://sysoev.ru/en/)

---

<div class="post-metadata">

**Author:** ![thomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e36b37/32.png) [@thomas](https://www.ruby-forum.com/u/thomas)\
**Post date:** [May 28, 2010, 5:20pm UTC](https://www.ruby-forum.com/t/nginx-logic-if-cookie-set-redirect-herea-is-it-possible/189317/3 "2010-05-28T17:20:29Z")

</div>

Igor S. wrote:

> On Fri, May 28, 2010 at 04:47:47PM +0200, Thomas Delonge wrote:
> 
> > proxy\_cache\_use\_stale error timeout invalid\_header updating  
> > http\_500 http\_502 http\_503 http\_504;
> > 
> > And it helps a ton. (instead of 15 requests per second it gets over  
> > 1000). Now I just need some sort of “server logic” to say only serve the  
> > cached page if they have no cookie, otherwise, load the dynamic page  
> > (which will automatically redirect them into the app).
> > 
> > Any ideas?
> 
> You should get the latest nginx version 0.8.38 and add:
> 
> ```
> proxy_no_cache $cookie_NAME;
> 
> ```
> 
> –  
> Igor S.  
> [Igor Sysoev](http://sysoev.ru/en/)

Thanks so much. I’ll install that version then.

I’ve had troubles understanding some things (with Nginx in general). I  
understand the general structure of the configuration and virtual hosts  
and such. But I haven’t really understood the variables. When you say  
$cookie\_NAME, does that mean I literally put that? Or Do I put  
$cookie\_SOMETHINGELSEHERE?

---

<div class="post-metadata">

**Author:** ![thomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e36b37/32.png) [@thomas](https://www.ruby-forum.com/u/thomas)\
**Post date:** [May 28, 2010, 5:25pm UTC](https://www.ruby-forum.com/t/nginx-logic-if-cookie-set-redirect-herea-is-it-possible/189317/4 "2010-05-28T17:25:14Z")

</div>

On Fri, May 28, 2010 at 05:20:29PM +0200, Thomas Delonge wrote:

> > >
> 
> Thanks so much. I’ll install that version then.
> 
> I’ve had troubles understanding some things (with Nginx in general). I  
> understand the general structure of the configuration and virtual hosts  
> and such. But I haven’t really understood the variables. When you say  
> $cookie\_NAME, does that mean I literally put that? Or Do I put  
> $cookie\_SOMETHINGELSEHERE?

If you cookie name is “user” then you should use $cookie\_user.

–  
Igor S.  
[http://sysoev.ru/en/](http://sysoev.ru/en/)
