Django + Nginx + Gunicorn"连接由同行重置"错误

时间:2017-07-17 22:07:21

标签: django nginx gunicorn

我正在使用数字海洋教程(here)来设置允许上传文件的应用(视频范围从5 MB到1 GB)。我知道大文件上传不是一个理想的用例,但是客户端和服务器都位于通过LAN连接的相邻建筑物中(传输速度很快),并且FTP不是我提供的选项。

当文件足够小(30-40 mb)时,应用程序运行正常。有100 MB的视频,我得到一个" 502 - 糟糕的网关"客户端的错误。

Nginx错误日志显示以下内容:

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"

Gunicorn错误日志显示没有错误。

我的手枪设置:

[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

我做错了什么?

编辑 - &gt;

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;
    } 
}

1 个答案:

答案 0 :(得分:0)

通过将工作类从龙卷风更改为默认(同步)工作程序,同时保持超时足够长时间以完成大型上载,解决了此问题。显然,这个问题可能有更优雅的解决方案,但我还没有遇到任何问题。 gunicorn.service中的exec行如下:

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