Choose upstream based in a cookie

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

Posted at Nginx Forum:

Hello,

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

        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:

I don’t know if that will work (see If is Evil… when used in location context | NGINX)

This will almost certainly work:

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: