Answer a question

I'm using the Digital Ocean tutorial (here) to set up a app that allows for a file upload (videos ranging from 5 mb to 1 GB). I know that large file uploads is not an ideal use case, but the client and the server are sitting in neighboring buildings connected via LAN (fast speeds in transfer) and FTP was not an option provided to me.

When the files are small enough (30-40 mb), the app works fine. With 100 mb+ videos, I get a "502 - bad gateway" error on the client side.

Nginx error log shows the following:

2017/07/17 15:52:18 [error] 18503#18503: *9 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: <client-ip>, server: <my-hostname>, request: "POST /videos HTTP/1.1", upstream: "http://unix:/www/app/app.sock:/videos", host: "<my-hostname>", referrer: "<app-domain>/videos"

The Gunicorn error log shows no errors.

My gunicorn settings:

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=django
Group=www-data
WorkingDirectory=/www/app
EnvironmentFile=/www/app/.env2 
ExecStart=/home/django/.pyenv/versions/django/bin/gunicorn --access-logfile /backup/logs/app_gunicorn_access.log --error-logfile /backup/logs/app_gunicorn_errors.log --workers 3 --worker-class=tornado  --timeout=600 --graceful-timeout=10 --log-level=DEBUG --capture-output --bind unix:/www/app/app.sock app.wsgi:application

[Install]
WantedBy=multi-user.target

What am I doing wrong?

EDIT ->

NGINX config

server {
    listen 80;
    server_name <my-hostname>;
    rewrite ^/(.*) https://<my-domain>/$1 permanent;
}

server {
    listen 443 ssl;
    proxy_read_timeout 600s;
    keepalive_timeout 5;
    fastcgi_buffers 8 16k;
    fastcgi_buffer_size 32k;
    server_name <my-hostname>;
    client_max_body_size 0;

    ssl_certificate /ssl_certs/hostname_bundle.cer;
    ssl_certificate_key /ssl_certs/hostname.key;

    root /www/app;

    location = /favicon.ico { access_log off; log_not_found off; }

    location /static/ {
        alias /www/app/staticfiles/;
    }

    location /media/ {
        alias /backup/app_media/;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/www/app/app.sock;
        include  /etc/nginx/mime.types;
    }

    location /robots.txt {
        alias /www/app/robots.txt;
    } 
}

Answers

Fixed the issue by changing the worker class from tornado to default (sync) workers while keeping the timeout long enough for large uploads to complete. Obviously there might be more elegant solutions to this issue, but I'm yet to come across anything. Exec line in gunicorn.service is as follows:

ExecStart=/home/django/.pyenv/versions/django/bin/gunicorn --access-logfile /backup/logs/app_gunicorn_access.log --error-logfile /backup/logs/app_gunicorn_errors.log --workers 3 --timeout=600 --graceful-timeout=10 --log-level=DEBUG --capture-output --bind unix:/www/app/app.sock app.wsgi:application
Logo

开发云社区提供前沿行业资讯和优质的学习知识,同时提供优质稳定、价格优惠的云主机、数据库、网络、云储存等云服务产品

更多推荐