title: intercepting upstream error with nginx
body: <p>My app runs with nginx and uwsgi (python). My aim is to drop a connection (as explained <a href="https://stackoverflow.com/questions/29300344/terminating-a-uwsgi-worker-programmatically">here</a>) e.g. when the python app decides to do so.</p>
  
  <p>Is there a nginx parameter to "intercept" upstream errors similar to <code>proxy_intercept_errors</code>? </p>
  
  <p>According to <a href="https://stackoverflow.com/questions/5031818/show-a-custom-503-page-if-upstream-is-down">this answer</a>, </p>
  
  <p>My nginx config:</p>
  
  <pre><code>location / {
      uwsgi_pass                myapp;
      include                   uwsgi_params;
  
      uwsgi_buffering           on;
      uwsgi_buffer_size         8k;
  
      uwsgi_connect_timeout     800;
  
      uwsgi_read_timeout        800;
      uwsgi_send_timeout        800;
  
      proxy_intercept_errors  on;
      error_page 420 =444 @foo;
  }
  
  location @foo {
      return 444;
  }
  </code></pre>
  
  <p>I tried all possible combinations I could think of with/without <code>proxy_intercept_errors</code> and <code>error_page</code> but no matter how I configure nginx, when I return <code>420 Enhance your calm</code> (or any other error code, apparently) it gets passed directly to the client.</p>
  
  <p><a href="https://serverfault.com/questions/279109/error-page-doesnt-seem-to-work-with-nginx-upstream-directive">This answer</a> suggests using <code>proxy_next_upstream</code> but that implies using <code>proxy_pass</code> rather than <code>uwsgi_pass</code>. I need to use <code>uwsgi_pass</code> because I need certain parameters passed to the python app.</p>