Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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
share|improve this question
Use HttpUpstreamRequestHashModule for the first layer – Gustav Jun 24 '13 at 15:08

1 Answer 1

Take the query string variable (eg. $arg_x) and plug it into

share|improve this answer
Thanks, but this is not exactly what I want. I want to decide the upstream server pool based on hash of $arg_x. If i use that module, I wont be able to use a pool but only a specific server. The problem would then be that I wont be able to use weights and backups which I think I want in my setup to make it full proof. – Sparsh Gupta Jan 22 '11 at 21:55

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.