title: nginx 502 bad gateway for long running request
body: <p>I have a request that takes "a few mins" to process. This works fine when I access it via Django's built in development server. </p>
  
  <p>However when I access it from nginx/uwsgi I get 502 bad gateway.</p>
  
  <p>I have tried increasing the timeout/keepalive settings in nginx.conf but to no effect.</p>
  
  <p>Here is the relevant conf setting:-</p>
  
  <pre><code>#keepalive_timeout  0;
  client_body_timeout   10;
  client_header_timeout 10;
  keepalive_timeout     5 5;
  send_timeout          10;
  </code></pre>
  
  <p>And nginx debug log errors:-</p>
  
  <pre><code>2016/02/03 17:35:33 [notice] 12654#0: nginx/1.4.2
  2016/02/03 17:35:33 [notice] 12654#0: built by gcc 4.4.7 20120313 (Red Hat 4.4.7-3) (GCC)
  2016/02/03 17:35:33 [notice] 12654#0: OS: Linux 2.6.32-358.14.1.el6.x86_64
  2016/02/03 17:35:33 [notice] 12654#0: getrlimit(RLIMIT_NOFILE): 1024:4096
  2016/02/03 17:35:33 [notice] 12655#0: start worker processes
  2016/02/03 17:35:33 [notice] 12655#0: start worker process 12657
  2016/02/03 17:35:33 [notice] 12655#0: start worker process 12658
  2016/02/03 17:35:33 [notice] 12655#0: start worker process 12659
  2016/02/03 17:35:33 [notice] 12655#0: start worker process 12660
  2016/02/03 17:36:36 [error] 12658#0: *12 upstream prematurely closed connection while reading response header from upstream, client: xxx.xxx.xxx.xxx, server: xxxxxxxx.in, request: "GET /long/url/with?request=params HTTP/1.1", upstream: "uwsgi://10.176.6.247:8001", host: "xxx.xxx.xxx.xxx"
  </code></pre>