title: Redirect to different upstream on error using nginx
body: <p><strong>Description</strong></p>
  
  <p>I have two different services/servers running locally, one uses php5.6-fpm running on port 9000, the other is uwsgi running on port 5000, I would like to be able to stop php5.6-fpm and have the request be redirected to the uwsgi service.</p>
  
  <pre><code>upstream phpfpm {
      server 127.0.0.1:9000 max_fails=1 fail_timeout=5s;
  }
  
  upstream darkapp {
      server 0.0.0.0:5000 max_fails=1 fail_timeout=5s;
  }
  
  
  
  server {
      listen 2000;
      index index.php;
  
      location ~ \.php$ {
          root /var/www/html;
          fastcgi_pass phpfpm;
          include fastcgi_params;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          fastcgi_param QUERY_STRING $query_string;
          fastcgi_connect_timeout 5s;
          error_page 404 502 = /dark/home;
          #error_page 404 = @fallback;
      }
  
      #location @fallback
      location = /dark/home {
          #include uwsgi_params;
          uwsgi_pass darkapp;
          #proxy_pass http://localhost:4000;
      }
  }
  
  server {
      listen 4000;
  
      location / { 
          include uwsgi_params;
          uwsgi_pass darkapp;
      }
  }
  </code></pre>
  
  <p><strong>Note</strong></p>
  
  <p>I am able to access php applications on port 2000 until I manually stop the fpm service, I am also able to access the python application on port 4000.</p>
  
  <p><strong>What Happens</strong></p>
  
  <p>When I stop php-fpm and I try to access a site on port 2000 through my browser, I get the following when i DON'T INCLUDE uwsgi_params</p>
  
  <pre><code>2019/02/04 14:27:54 [error] 21062#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1, server: , request: "GET /info/ HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "localhost:2000"
  
  
  2019/02/04 14:27:54 [error] 21062#0: *1 upstream prematurely closed connection while reading response header from upstream, client: 127.0.0.1, server: , request: "GET /info/ HTTP/1.1", upstream: "uwsgi://0.0.0.0:5000", host: "localhost:2000"
  </code></pre>
  
  <p>Also the Uwsgi process which runs a flask app gives the following</p>
  
  <pre><code>Traceback (most recent call last):
    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 2309, in __call__
      return self.wsgi_app(environ, start_response)
    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 2287, in wsgi_app
      ctx = self.request_context(environ)
    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 2204, in request_context
      return RequestContext(self, environ)
    File "/usr/local/lib/python2.7/dist-packages/flask/ctx.py", line 284, in __init__
      self.url_adapter = app.create_url_adapter(self.request)
    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 2021, in create_url_adapter
      subdomain=subdomain)
    File "/usr/local/lib/python2.7/dist-packages/werkzeug/routing.py", line 1344, in bind_to_environ
      environ['REQUEST_METHOD'], path_info,
  KeyError: 'REQUEST_METHOD'
  </code></pre>
  
  <p>When i DO INCLUDE uwsgi params, it redirects to / and I get the following</p>
  
  <pre><code>2019/02/04 14:42:34 [error] 22493#0: *43 connect() failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1, server: , request: "GET /info/ HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "localhost:2000"
  2019/02/04 14:42:34 [error] 22493#0: *43 directory index of "/usr/share/nginx/html/" is forbidden, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "localhost:2000"
  </code></pre>