Nginx – How to make nginx understand dots in cookie values

cookiesmappingnginx

I have Nginx configured as a simple sticky LB. One of the key parts of config is matching cookie value to server address. This works fine:

map $cookie_sessionServer $http_sticky_backend {
    default 0;
    Server_A   192.168.73.210:1337;
    Server_B   192.168.73.210:1338;
}

Now I have to add dots to cookie values (why is another story). But when I change the config, it stops working.

map $cookie_sessionServer $http_sticky_backend {
    default 0;
    .Server_A   192.168.73.210:1337;
    .Server_B   192.168.73.210:1338;
}

No errors, nothing useful in debug log, this map just "returns" the default value (0) and stickiness logic is skipped.

I tried also

\.Server_A 192.168.73.210:1337;

and

".Server_A" 192.168.73.210:1337;

and

~^\.Server_A$ 192.168.73.210:1337;

and

~\.Server_A$ 192.168.73.210:1337;

but none of this worked for me =(

Best Answer

If you want to use sticky sessions, alternative modules are available to avoid nginx plus commercial implementation :

You'll need to recompile nginx but that's not tough.

You can also use tengine, an nginx open source fork also implementing sticky sessions.

If you absolutely want to do it this way, you'll lose advantages of load balancing when cookie is missing until you write ugly if blocks catching this exception. Also if you set upstream checks with max_fails and fail_timeout you'll loose upstream status pooling advantages and end up attempting to forward requests to servers suffering breakdowns.

For the record here's the way you can do it partially work (considering what I just told) with map directive :

map $cookie_sessionServer $route {
    default 0;
    "~\.Server_A$" 192.168.73.210:1337;
    "~\.Server_B$" 192.168.73.210:1338;
}

server {

    [ ... ]

    location /foo {
        proxy_pass http://$route;
    }

}

Tested with nginx 1.6.2