nginx hashing on GET parameter

Posted by Sparsh Gupta on Server Fault See other posts from Server Fault or by Sparsh Gupta
Published on 2011-01-22T21:13:04Z Indexed on 2012/09/13 15:40 UTC
Read the original article Hit count: 211

Filed under:

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-

  1. What is the nginx equivalent of such a code. I dont know if nginx accepts modulas
  2. 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
  3. Any other alternate way to solve this problem

© Server Fault or respective owner

Related posts about nginx