0

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 =(

  • Why are you doing this at all? nginx already has a `sticky` directive to handle this for you. – Michael Hampton Oct 01 '14 at 12:29
  • Really? What I know about is a third-party sticky module which requires Nginx to be rebuilt with its support. What I need to do is to provide sample LB configs for most popular servers (Nginx, Apache) for our app. So I would like to avoid custom builds or third-party plugins. – Georgii Ivankin Oct 01 '14 at 13:09
  • Aha, nevermind, it is part of "nginx plus" the commercial product. – Michael Hampton Oct 01 '14 at 13:14
  • 1
    @GeorgiyIvankin Trying to implement your own sticky module with maps is a poor choice. Better recompile nginx with robust modules and let your configuration clear. – Xavier Lucas Oct 01 '14 at 14:21

1 Answers1

2

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

Xavier Lucas
  • 12,815
  • 2
  • 44
  • 50
  • Thanks for the answer! I already have working LB configuration for basic Nginx, with exactly the same flaws you'd mentioned. However, my goal is not to create a working production config, but to give customers an example of how they could configure their servers to LB our product. The most popular tools in use are Nginx and Apache. But maybe I really should consider advising customers to recompile Nginx with extra modules or to use tengine, because the benefits are evident. – Georgii Ivankin Oct 02 '14 at 06:38