title: Load balancing of process load balancer
body: <p>I have a loadbalancer named LB1, which balance between my 2 ip when it receive a request on *.domain.com. I have put a max_fails and a fail_timeout in order to redirect trafic on the other when one is down :</p>
  
  <pre><code>upstream upstreamdomain {
  
     least_conn;
  
  
     server X.X.X.1 max_fails=1 fail_timeout=10s;
     server X.X.X.2 max_fails=1 fail_timeout=10s;
  
   }
  
  
  server {
  
      listen 80;
      server_name ~^(?&lt;subdomain&gt;.+)\.domain.com;
  
  
  
      location / {
  
          proxy_set_header Host $subdomain.domain.com;
  
          proxy_pass http://upstreamdomain;
  
      }
  
  }
  </code></pre>
  
  <p>On each X.X.X.1 and X.X.X.2 i have my applications running on Y process, and i manage the loadbalancing between process with nginx too</p>
  
  <pre><code>upstream defaultupstream {
      least_conn;
  
  
      server 127.0.0.1:9000 max_fails=1 fail_timeout=10s;
      server 127.0.0.1:9001 max_fails=1 fail_timeout=10s;
      server 127.0.0.1:9002 max_fails=1 fail_timeout=10s;
   }
  server {
      listen 80;
      server_name ~^(?&lt;subdomain&gt;.+)\.domain.com;
  
  
  
      location / {
         proxy_set_header Host $subdomain.domain.com;
         proxy_pass http://defaultupstream;
      }
   }
  </code></pre>
  
  <p>When a server is crashing down i want that LB1 redirect trafic on the other, and <strong>it works well</strong>.</p>
  
  <p>When a process is crashing on X.X.X.1 or X.X.X.2 i want that nginx redirect trafic on other process, and <strong>it works well</strong>.</p>
  
  <p>The problem i have its when all process are crashing on X.X.X.1 or X.X.X.2 the nginx on X.X.X.1 or X.X.X.2 return 502 server unavailable</p>
  
  <p>It seems that the 502 response not increasing the max_fails on LB1, LB1 is still considering the server with no working process as alive.</p>
  
  <p><strong>How can i configure nginx on LB1 in order to eval 502 as a fail ?
  If its not possible, on X.X.X.1 and X.X.X.2 what i have to repond (503, 504 . ?) in order that my LB1 consider the server as down ?</strong></p>
  
  <p><strong>---------------------- EDIT ------------------</strong></p>
  
  <p>Thanks to <strong>oklas</strong>, fix by adding the following line in LB1 configuration, in order to force nginx to change the upstream on error, timeout or http 502, 503 and 504: </p>
  
  <pre><code>proxy_next_upstream error timeout http_502 http_503 http_504;
  </code></pre>
  
  <p>LB1 conf is now : </p>
  
  <pre><code>upstream upstreamdomain {
  
     least_conn;
  
  
     server X.X.X.1 max_fails=1 fail_timeout=10s;
     server X.X.X.2 max_fails=1 fail_timeout=10s;
  
   }
  
  
  server {
  
      listen 80;
      server_name ~^(?&lt;subdomain&gt;.+)\.domain.com;
  
  
  
      location / {
  
          proxy_set_header Host $subdomain.domain.com;
  
          proxy_pass http://upstreamdomain;
          proxy_next_upstream error timeout http_502 http_503 http_504;
      }
  
  }
  </code></pre>
  
  <p>Thanks in advance.
  Regards</p>