I've set up NGINX to load-balance requests to my web app. One front end server passes requests to two backend servers. All works fine.

However, I have been using the ip_hash directive to ensure users are served by the same server each time. This doesn't seem to be working - requests alternate between the two backend servers, and this is causing problems with user sessions.

The config should be quite simple, so I don't know where this is going wrong - any ideas appreciated.

upstream webservers  {
  server node1.mysite.com;
  server node2.mysite.com;
  server localhost:8080 backup;

server {
    listen 80;

    server_name www.mysite.com;

    location / {

        proxy_set_header    Host                $host;
        proxy_set_header    X-Real-IP           $remote_addr;
        proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
        proxy_set_header    X-Forwarded-Proto   $scheme;
        proxy_pass  http://webservers;


  • Can you post factual information about your issue ? Like the relevant access logs ... – Xavier Lucas Nov 26 '14 at 22:22

I found a solution, of sorts.

It seems that ip_hash is incompatible with the backup directive, and possibly with any other directive in the upstream block.

Once I removed the backup line, ip_hash works as it should.

However, this seems strange. I'm using a recent version of NGINX - 1.6 - and I can't find anything in the more recent docs to say that ip_hash and backup directives are still incompatible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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