title: Django+gunicorn+nginx upload large file 502 error
body: <h1>Problem</h1>
  
  <p>Uploading 1-2mb files works fine.
  When I attempt to upload 16mb file, i get 502 error after several seconds</p>
  
  <p>More detalied:</p>
  
  <ol>
  <li>I click "Upload"</li>
  <li>Google Chrome uploads file (upload status is changing from 0% to 100% in left bottom corner)</li>
  <li>Status changes to "Waiting for HOST", where HOST is my site hostname</li>
  <li>After a half of minute server returns "502 Bad Gateway"</li>
  </ol>
  
  <h3>My view:</h3>
  
  <pre><code>def upload(request):
      if request.method == 'POST':
          f = File(data=request.FILES['file'])
          f.save()
          return redirect(reverse(display),  f.id)
      else:
          return render('filehosting_upload.html', request)
  </code></pre>
  
  <p>render(template, request [,data]) is my own shorthand that deals with some ajax stuff;</p>
  
  <p>The <code>filehosting_upload.html</code>:</p>
  
  <pre><code>{% extends "base.html" %}
  {% block content %}
      &lt;h2&gt;File upload&lt;/h2&gt;
      &lt;form action="{% url nexus.filehosting.views.upload %}" method="post" enctype="multipart/form-data"&gt;
          {% csrf_token %}
          &lt;input type="file" name="file"&gt;
          &lt;button type="submit" class="btn"&gt;Upload&lt;/button&gt;
      &lt;/form&gt;
  {% endblock %}
  </code></pre>
  
  <h1>Logs &amp; specs</h1>
  
  <p>There are nothing informative in logs i can find.</p>
  
  <p>Versions:</p>
  
  <ul>
  <li>Django==1.4.2 </li>
  <li>Nginx==1.2.1 </li>
  <li>gunicorn==0.17.2</li>
  </ul>
  
  <p>Command line parameters</p>
  
  <pre><code>command=/var/www/ernado/data/envs/PROJECT_NAME/bin/gunicorn -b localhost:8801 -w 4 PROJECT_NAME:application
  </code></pre>
  
  <p>Nginx configuration for related location:</p>
  
  <pre><code>   location /files/upload {
      client_max_body_size 100m;
      proxy_pass http://HOST;
      proxy_connect_timeout 300s;
      proxy_read_timeout 300s;
     }
  </code></pre>
  
  <h3>Nginx log entry (changed MY_IP and HOST)</h3>
  
  <pre><code>2013/03/23 19:31:06 [error] 12701#0: *88 upstream prematurely closed connection while reading response header from upstream, client: MY_IP, server: HOST, request: "POST /files/upload HTTP/1.1", upstream: "http://127.0.0.1:8801/files/upload", host: "HOST", referrer: "http://HOST/files/upload"
  </code></pre>
  
  <h3>Django log</h3>
  
  <pre><code>2013-03-23 19:31:06 [12634] [CRITICAL] WORKER TIMEOUT (pid:12829)
  2013-03-23 19:31:06 [12634] [CRITICAL] WORKER TIMEOUT (pid:12829)
  2013-03-23 19:31:06 [13854] [INFO] Booting worker with pid: 13854
  </code></pre>
  
  <h1>Question(s)</h1>
  
  <ol>
  <li>how to fix that?</li>
  <li>is it possible to fix that without nginx upload module?</li>
  </ol>
  
  <p><strong>Update 1</strong>
  Tried suggested config </p>
  
  <pre><code> gunicorn --workers=3 --worker-class=tornado  --timeout=90 --graceful-timeout=10 --log-level=DEBUG --bind localhost:8801 --debug
  </code></pre>
  
  <p>Works fine for me now.</p>