Valentin
Valentin

Reputation: 1654

nginx load balancer/location puzzle

I would like to setup nginx as a load balancer. But I want to set it up in a way that certain requests (with specific parameter) will only go to certain hosts. Basically the idea is to use any host on original request, then if user specify certain paramter, e.g. bla0, then redirect requests to host 0, while for bla1 to host 1. So here is configuration I come up with:

# load balancing server
server {
    listen 8000;
    server_name example.com www.example.com;

    # requests to bla0 server
    location ~ ^(/request).*bla0$ {
        proxy_pass  http://localhost:8081;
    }

    # requests to bla1 server
    location ~ ^(/request).*bla1$ {
        proxy_pass  http://localhost:8082;
    }
    # for default location use balancer
    location / {
        proxy_pass  http://cluster;
    }

}

upstream cluster {
    server localhost:8081;
    server localhost:8082;
}

But unfortunately this configuration does not work. I always get round-robin requests, i.e. /request?q=bla0 goes to either host. What am I missing.

Upvotes: 1

Views: 286

Answers (1)

Alex Howansky
Alex Howansky

Reputation: 53636

Location doesn't match parameters. From http://wiki.nginx.org/HttpCoreModule#location

The location directive only tries to match from the first / after the hostname, to just before the first ? or #. (Within that range, it matches the unescaped url.)

Looks like you'll need to use the arg_* directive inside an if() but I'm not positive about that. Ah here we go, this looks like what you want.

Upvotes: 1

Related Questions