Nginx Gunicorn Django -- upstream prematurely closed connection 错误

Nginx Gunicorn Django -- upstream prematurely closed connection Error

我对 NGINX、GUNICORN、DJANGO 设置还很陌生。我在 nginx、gunicorn 之间使用 supervisor。在没有 NGINX 的情况下,设置可以很好地与 supervisor 和 gunicorn 配合使用,我可以通过我的服务器 IP 查看结果。但是当我使用 nginx 来处理请求时,出现错误 "upstream prematurely closed connection while reading response header from upstream"。请有人帮助我吗?

Supervisor command I am using:
sudo /path/to/gunicorn/gunicorn -k gevent --workers 4  --bind unix:/tmp/gunicorn.sock --chdir /path/to/application wsgi:application --timeout 120

下面是我目前正在使用的 nginx.conf,它按预期工作。但我不确定它是否符合要求。请调查一下。谢谢

==============更新=============

upstream xxxx {
    server unix:/tmp/gunicorn.sock;
}

server{

    listen 80;
    listen [::]:80;
    server_name xxx.in www.xxx.in;
    return 301 https://$host$request_uri;
}

    server{
         listen 443 ssl;
         ssl_certificate /etc/letsencrypt/live/xxx.in/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/xxx.in/privkey.pem;

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        ssl_ciphers 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
        location = /favicon.ico { access_log off; log_not_found off; }
            location /static/ {
                root path/to/project;
        }

        location / {
            include         uwsgi_params;
            proxy_pass      http://unix:/tmp/gunicorn.sock;
        } 

    }

请检查以下步骤:

  • 首先要确保 gunicorn 正在创建 .sock 文件 运行 主管。您可以使用

    来确保它
    $sudo supervisorctl status <name-of-supervisor-task-for-it>
    (check if service is RUNNING)
    
    $ls /tmp    
    (There should be a gunicorn.sock file existing there)
    
  • 还要注意您分配给主管配置的用户。在这种情况下,您不需要在命令之前设置 root,只需将 root 用户的权限授予配置文件即可。像这样:

    [program:myprogram]
    command=/path/to/gunicorn/gunicorn -k gevent --workers 4 --bind unix:/tmp/gunicorn.sock --chdir /path/to/application wsgi:application --timeout 120
    <other commands>
    user=root
    
  • 你的 nginx 配置应该是这样的:

    upstream django {
    server unix://tmp/gunicorn.sock;
    }
    
    
    server {
    listen 80;
    server_name <your_app_domain_here>;
    location / {
    include uwsgi_params;
    proxy_pass http://django/;
    
    }