Routing based on cookies

Hi guys!

I have Nginx configured as load balancer to some number of application
servers. I need to configure Nginx in such a way that if there is
cookie set in request (“server_id=<some_number>”), I have to forward
this request to backend identified by <some_number> - if there is no
cookie set, every server could be chosen with equal probability to
serve request.

So my need is something similar to functionality given by nginx
upstream hash module. But I want to have more control of which server
has to be chosen. It has to be exact server pointed by variable (in
this case: cookie value) - no hash calculations.

Something like this:

upstream {
server app-1;
server app-2;
server app-3;
choose $cookie_SERVER_ID;
}

When cookie “SERVER_ID” is present in request and it is number from 1
to 3 (which means order in server listed in config or suffix in server
name), it should suggest that this particular request should be
forwarded to app- server. But when there is no cookie, standard
round-robin algorithm could be used for balancing. Also when some
server is dead (even if it is chosen by variable from cookie) request
should be forwarded to next live backend.

How to do this? Is writing own module (similar to upstream hash) the
simplest idea? Or is there something more clever?

Best regards,
Kamil

ngx_lua(http://wiki.nginx.org/NginxHttpLuaModule) should meet your need.

在 2011-08-14日的 22:40 +0200,Kamil G.写道:

2011/8/14 Kamil G. [email protected]:

Hi guys!

Hi

has to be chosen. It has to be exact server pointed by variable (in

When cookie “SERVER_ID” is present in request and it is number from 1
to 3 (which means order in server listed in config or suffix in server
name), it should suggest that this particular request should be
forwarded to app- server. But when there is no cookie, standard
round-robin algorithm could be used for balancing. Also when some
server is dead (even if it is chosen by variable from cookie) request
should be forwarded to next live backend.

How to do this? Is writing own module (similar to upstream hash) the
simplest idea? Or is there something more clever?

nginx sticky module (Google Code Archive - Long-term storage for Google Code Project Hosting.)
does exactly what you’re looking for.

++ Jerome

2011/8/15 Jrme L. [email protected]:

cookie set, every server could be chosen with equal probability to
server app-1;
server is dead (even if it is chosen by variable from cookie) request
should be forwarded to next live backend.

How to do this? Is writing own module (similar to upstream hash) the
simplest idea? Or is there something more clever?

nginx sticky module (Google Code Archive - Long-term storage for Google Code Project Hosting.)
does exactly what you’re looking for.

Thanks! It looks great :slight_smile:

++ Jerome

Kamil

Hi!

On Mon, Aug 15, 2011 at 1:17 AM, leepengg [email protected] wrote:

ngx_lua(http://wiki.nginx.org/NginxHttpLuaModule) should meet your need.

Yeah, I found this before but it looks that is not enough for me. Of
course I can choose exact backend using lua (sth. similar to this:
OpenResty® - Open source) but if this backend
fails to respond I have no cotrol what nginx should do next, i.e it
will return error and not - as I wanted - try another server.

It looks that a lot of modules could be raplaced by lua scripts, but
as far as I understand not ‘load-balancer’ modules like ip_hash or
upstream_hash. Am I right?

Regards,
Kamil