Nginx + Django + Gunicorn:从上游读取响应标头时 502 Bad Gateway 上游过早关闭连接

And*_*ina 5 django nginx gunicorn

我正在创建(第一次)一个网站。更具体地说,用户上传图像,经过一些计算后,网站会显示一些结果。

但是,有时(并非总是如此,有时只是重试或更改其有效的图像)在此过程中我会收到“502 Bad Gateway nginx/1.10.3 (Ubuntu)”

查看我得到的错误:

[error] 7836#7836: *16 upstream prematurely closed connection while reading response header from upstream, client: 131.175.147.1, server: _, request: "POST / HTTP/1.1", upstream: "http://unix:/home/django/gunicorn.socket:/", host: "165.227.170.231", referrer: "http://165.227.170.231/"
Run Code Online (Sandbox Code Playgroud)

我试过这个,但它没有多大帮助,虽然我认为不是我的情况。

设置是:

/etc/nginx/sites-available/django :

upstream app_server {
server unix:/home/django/gunicorn.socket fail_timeout=0;
}

server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;

root /usr/share/nginx/html;
index index.html index.htm;

client_max_body_size 4G;
server_name _;

keepalive_timeout 5;

# Your Django project's media files - amend as required
location /media  {
    alias /home/django/django_project/django_project/media;
}   

# your Django project's static files - amend as required
location /static {
    alias /home/django/django_project/django_project/static;
}   

# Proxy the static assests for the Django Admin panel
location /static/admin {
   alias /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin/;
}  

location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
        proxy_buffering off;

        proxy_connect_timeout 75s;
        proxy_read_timeout 300s;



        proxy_pass http://app_server;
        }
}
Run Code Online (Sandbox Code Playgroud)

/etc/nginx/nginx.conf:

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
   ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

    proxy_connect_timeout 75s;
    proxy_read_timeout 300s;
  }


  #mail {
  #       # See sample authentication script at:
  #       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
  #
  #       # auth_http localhost/auth.php;
  #       # pop3_capabilities "TOP" "USER";
  #       # imap_capabilities "IMAP4rev1" "UIDPLUS";
  #
  #       server {
  #               listen     localhost:110;
  #               protocol   pop3;
  #               proxy      on;

  #       }
  #}
Run Code Online (Sandbox Code Playgroud)

/ect/gunicorn.d/gunicorn.py:

"""gunicorn WSGI server configuration."""
from multiprocessing import cpu_count
from os import environ


def max_workers():
    return cpu_count() * 2 + 1

    max_requests = 1000
    worker_class = 'gevent'
    workers = max_workers()
    TIMEOUT=120
Run Code Online (Sandbox Code Playgroud)

我以这种方式启动 gunicorn:

service gunicorn start --timeout 300 
Run Code Online (Sandbox Code Playgroud)

基本上,我到处尝试避免超时,但仍然遇到同样的问题。