nginx hashing on GET parameter
- by Sparsh Gupta
I have two Varnish servers and I plan to add more varnish servers. I am using a nginx load balancer to divide traffic to these varnish servers. To utilize maximum RAM of each varnish server, I need that same request reaches same varnish server. Same request can be identified by one GET parameter in the request URL say 'a'
In a normal code, I would do something like-
(if I need to divide all traffic between 2 Varnish servers)
if($arg_a % 2 == 0) {
proxy_pass varnish1;
}
if($arg_a % 2 == 1) {
proxy_pass varnish2;
}
This is basically doing a even / odd check on GET parameter a and then deciding which upstream pool to send the request. My question are-
What is the nginx equivalent of such a code. I dont know if nginx accepts modulas
Is there a better/ efficient hashing function built in with nginx (0.8.54) which I can possibly use. In future I want to add more upstream pools so I need not to change %2 to %3 %4 and so on
Any other alternate way to solve this problem