# Choose upstream based in a cookie

**URL:** <https://www.ruby-forum.com/t/choose-upstream-based-in-a-cookie/202480>\
**Category:** NGINX\
**Created:** [January 30, 2011, 12:15pm UTC](https://www.ruby-forum.com/t/choose-upstream-based-in-a-cookie/202480 "2011-01-30T12:15:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [January 30, 2011, 12:15pm UTC](https://www.ruby-forum.com/t/choose-upstream-based-in-a-cookie/202480/1 "2011-01-30T12:15:37Z")

</div>

Hello,

I was reading this setup: [http://forum.nginx.org/read.php?11,1259](http://forum.nginx.org/read.php?11,1259) and it  
does what I want, but is this the way to go or there is a better way?  
Ame say we should avoid if else?

Thanks

Posted at Nginx Forum:

> **[Choose upstream based in a cookie](https://forum.nginx.org/read.php?2,170775,170775#msg-170775)**
>
> Hello, I was reading this setup: http://forum.nginx.org/read.php?11,1259 and it does what I want, but is this the way to go or there is a better way? Ame say we should avoid if else? Thanks

---

<div class="post-metadata">

**Author:** ![nfn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b19c9b/32.png) [@nfn](https://www.ruby-forum.com/u/nfn)\
**Post date:** [February 17, 2011, 12:12am UTC](https://www.ruby-forum.com/t/choose-upstream-based-in-a-cookie/202480/2 "2011-02-17T00:12:05Z")

</div>

Hello,

Is this the right way to switch backend based in cookies?

```auto
        location / {
                if ($http_cookie ~* "memberid=") {
                        proxy_pass http://10.10.10.10;
                        break;
                }
                if ($http_cookie ~* "clientid=") {
                        proxy_pass http://10.10.10.15;
                        break;
                }
                proxy_pass http://10.10.10.20;
        }
```

Thanks.

Posted at Nginx Forum:

> **[Re: Choose upstream based in a cookie](https://forum.nginx.org/read.php?2,170775,176001#msg-176001)**
>
> Hello, I was reading this setup: http://forum.nginx.org/read.php?11,1259 and it does what I want, but is this the way to go or there is a better way? Ame say we should avoid if else? Thanks

---

<div class="post-metadata">

**Author:** ![nfn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b19c9b/32.png) [@nfn](https://www.ruby-forum.com/u/nfn)\
**Post date:** [February 17, 2011, 4:25pm UTC](https://www.ruby-forum.com/t/choose-upstream-based-in-a-cookie/202480/3 "2011-02-17T16:25:05Z")

</div>

I don’t know if that will work (see [If is Evil… when used in location context | NGINX](http://wiki.nginx.org/IfIsEvil))

This will almost certainly work:

```auto
location / {
    set_by_lua $backend_ip '
      if ngx.var.cookie_memberid ~= "" and ngx.var.cookie_memberid ~= 
nil
then
        return "10.10.10.10"
      elseif ngx.var.cookie_clientid ~= "" and ngx.var.cookie_clientid 
~=
nil then
        return "10.10.10.15"
      else
        return "10.10.10.20"
      end
    ';
    proxy_pass http://$backend_ip;
}
```

Needs 3rd party lua module.

I only just discovered it myself and starting to learn lua. Seems worth  
it as it opens up a lot of possibilities without getting into the if is  
evil issues.

Posted at Nginx Forum:

> **[Re: Choose upstream based in a cookie](https://forum.nginx.org/read.php?2,170775,176154#msg-176154)**
>
> Hello, I was reading this setup: http://forum.nginx.org/read.php?11,1259 and it does what I want, but is this the way to go or there is a better way? Ame say we should avoid if else? Thanks
