Django + gunicorn + nginx download large file 502 error

Problem

Downloading 1-2 MB files is great. When I try to download a 16mb file, I get a 502 error after a few seconds

In details:

  • I click download
  • Google Chrome downloads the file (download status changes from 0% to 100% in the lower left corner)
  • The status changes to "Pending HOST", where HOST is the name of my site
  • After half a minute server returns "502 Bad Gateway"

My opinion:

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) 

render (template, request [, data]) is my own shorthand that affects some ajax stuff;

filehosting_upload.html :

 {% extends "base.html" %} {% block content %} <h2>File upload</h2> <form action="{% url nexus.filehosting.views.upload %}" method="post" enctype="multipart/form-data"> {% csrf_token %} <input type="file" name="file"> <button type="submit" class="btn">Upload</button> </form> {% endblock %} 

Magazines and Specifications

There is nothing informative in the magazines.

Versions:

  • Django == 1.4.2
  • Nginx == 1.2.1
  • gunicorn == 0.17.2

Command line options

 command=/var/www/ernado/data/envs/PROJECT_NAME/bin/gunicorn -b localhost:8801 -w 4 PROJECT_NAME:application 

Nginx configuration for the appropriate location:

  location /files/upload { client_max_body_size 100m; proxy_pass http://HOST; proxy_connect_timeout 300s; proxy_read_timeout 300s; } 

Nginx log entry (modified by MY_IP and HOST)

 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" 

Django Magazine

 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 

Question (s)

  • How to fix it?
  • Can I fix this without the nginx download module?

Update 1 Tried the proposed configuration

  gunicorn --workers=3 --worker-class=tornado --timeout=90 --graceful-timeout=10 --log-level=DEBUG --bind localhost:8801 --debug 

Now works great for me.

+4
source share
2 answers

I run my action movie with these parameters, try:

 python manage.py run_gunicorn --workers=3 --worker-class=tornado --timeout=90 --graceful-timeout=10 --log-level=DEBUG --bind 127.0.0.1:8151 --debug 

or if you work differently, you can run with these parameters

+3
source

You need to use a class of another working async type, such as gevent or tornado , to learn more: First explanation:

You can also install Eventlet or Gevent if you expect your application code to need to be paused for long periods of time when processing a request

Second:

Synchronous workstations, by default, assume that your application is tied to resources on processor and network bandwidth. Typically, this means that your application should not do anything that takes undefined time. For example, an Internet request meets these criteria. At some point, the external network will fail in such a way that clients will accumulate on your servers.

0
source

All Articles