【问题标题】:Django gunicorn nginx (111: Connection refused) while connecting to upstreamDjango gunicorn nginx(111:连接被拒绝)同时连接到上游
【发布时间】:2017-10-18 04:48:44
【问题描述】:

AWS 实例上运行一个 Django 应用,通过 gunicorn 和 nginx 配置,运行了一年多,但突然出现 502 bad gateway 错误,然后我在 nginx 错误中看到了下面提到的消息记录,

2017/05/17 16:18:35 [error] 1040#0: *7460 connect() to unix:/home/ubuntu/webapps/myproject/myproject/myproject.sock failed (111: Connection refused) while connecting to upstream, client: xx.xxxx.xx.xxx, server: xx.xx.xx.xxx, request: "GET / HTTP/1.1", upstream: "http://unix:/home/ubuntu/webapps/myproject/myproject/myproject.sock:/", host: "xx.xx.xx.xxx", referrer: "http://xx.xx.xx.xxx"

我的 nginx 配置:

server {
        client_max_body_size 200M;
        listen 80;
        listen [::]:80 ipv6only=on;
        server_name xx.xx.xx.xxx;
        listen 443 ssl;
        ssl_certificate /etc/nginx/ssl/myserver.crt;
        ssl_certificate_key /etc/nginx/ssl/myserver.key;


        location = /favicon.ico { access_log off; log_not_found off; }
        location /static/ {
                root /home/ubuntu/webapps/myproject/myproject;
        }

        location / {
                include proxy_params;
                proxy_pass http://unix:/home/ubuntu/webapps/myproject/myproject/myproject.sock;
                proxy_set_header X-Forwarded-Protocol $scheme;
        }

        if ($scheme = http){
                return 301 https://xx.xx.xx.xxx$request_uri;
        }

        if ($http_host = pas-cash.com){
                return 303 https://xx.xx.xx.xxx$request_uri;
        }
}

我的 gunicorn.conf

description "Gunicorn application server handling myproject"

start on runlevel [6789]
stop on runlevel [!6789]

respawn
setuid ubuntu
setgid www-data
chdir /home/ubuntu/webapps/myproject/myproject

exec /home/ubuntu/webapps/myproject/venv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/webapps/myproject/myproject/myproject.sock myproject.wsgi:application

之后我通过以下命令重新启动了 nginx

sudo service nginx restart

重新启动后,应用程序运行良好,我找不到这个错误背后的具体原因,我为此搜索了它,但我得到了不同类型的答案,但没有适合我的答案,请你们帮帮我,为什么会发生这种情况,我的配置中是否缺少任何东西,或者这种行为背后的常见/一般原因是什么。这对我很有帮助,在此先感谢。

【问题讨论】:

  • 尝试从nginx配置中的proxy_pass中删除http://
  • @alfonso.kim 感谢您的评论,您能否解释一下为什么会发生这种情况,以及从“proxy_pass”中删除“http://”的原因是什么,请给我一个清晰的解释。
  • 当然。我不确定这是否是您问题的答案,所以我没有发布它。成功了吗?
  • @alfonso.kim,实际上应用程序在我重新启动 nginx 后运行良好,我需要知道它为什么会发生以及这背后的原因是什么。就是这样。
  • 你是怎么让它工作的? Nginx 不喜欢我。 nginx: [emerg] invalid URL prefix

标签: django nginx gunicorn


【解决方案1】:

这个问题有几个原因,在我的例子中是因为服务没有启用。运行sudo systemctl enable gunicorm.service 后修复它。

【讨论】:

    【解决方案2】:

    这是“突然”引起的,不是因为 nginx 错误,而是 gunicorn 或您的应用程序错误(代码错误、未安装软件包等)。不过,它应该相对容易记录和修复。

    首先尝试从服务器 python manage.py runserver 运行您的应用程序,看看您是否遇到任何问题。 ... migrate 也是如此。通常,生产不工作但本地工作的问题是由于缺少包或缺少迁移。在本地创建一个 requirements.txt 文件并在生产环境中安装它。

    如果错误仍然存​​在,请使用 gunicorn --log-file=- YourApp.wsgi:application 检查 gunicorn 日志。一旦所有这些错误都得到纠正,运行

    sudo systemctl status gunicorn.socket
    sudo systemctl status gunicorn
    

    并且您希望既活跃又运行。如果您开始收到 400 错误,这是一个好兆头,因为它现在是 Django 错误(通常是允许的主机)。打开 debug=True 以查看来自 django 的确切错误。

    记住每当对代码运行进行任何更改时

    sudo systemctl daemon-reload
    sudo systemctl restart gunicorn
    

    仅供参考,如果上述方法都不起作用,那么您可以随时检查您的 nginx 日志

    sudo tail -30 /var/log/nginx/error.log
    

    【讨论】:

    • 有助于提醒基础知识。
    【解决方案3】:

    尝试从 nginx 配置中的 proxy_pass 中删除 http://

    server {
        client_max_body_size 200M;
        listen 80;
        listen [::]:80 ipv6only=on;
        server_name xx.xx.xx.xxx;
        listen 443 ssl;
        ssl_certificate /etc/nginx/ssl/myserver.crt;
        ssl_certificate_key /etc/nginx/ssl/myserver.key;
    
    
        location = /favicon.ico { access_log off; log_not_found off; }
        location /static/ {
                root /home/ubuntu/webapps/myproject/myproject;
        }
    
        location / {
                include proxy_params;
                proxy_pass unix:/home/ubuntu/webapps/myproject/myproject/myproject.sock;
                proxy_set_header X-Forwarded-Protocol $scheme;
        }
    
        if ($scheme = http){
                return 301 https://xx.xx.xx.xxx$request_uri;
        }
    
        if ($http_host = pas-cash.com){
                return 303 https://xx.xx.xx.xxx$request_uri;
        }
    }
    

    原因是 gunicorn 正在侦听 unix 套接字(--bind 参数)。然后nginx 应该将流量转发到这个套接字。 http:// 代表常规 IP:PORT 中的 TCP 套接字,这不是你的情况。

    【讨论】:

    • 感谢您的解释。我能理解这个问题。你能告诉我 nginx 没有将流量转发到套接字的可能原因是什么(比如,任何网络中断或类似的事情。)
    • 哼,那还是不行?请确保 gunicorn 和 nginx 都已启动并运行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-03
    • 2018-04-07
    • 2016-10-06
    • 1970-01-01
    • 2014-02-26
    • 2012-11-21
    • 2020-04-09
    相关资源
    最近更新 更多