Nginx + Django + Gunicorn:502错误的网关上游过早关闭的连接,同时从上游读取响应标头

时间:2018-07-14 10:55:32

标签: 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/"

我尝试了this,但并没有太大帮助,尽管我认为this不是我的情况。

设置为:

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

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

  #       }
  #}

/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

然后我以这种方式开始使用金枪鱼:

service gunicorn start --timeout 300 

基本上,我尽力避免超时,但仍然遇到相同的问题。

0 个答案:

没有答案