【问题标题】:connect() failed (111: Connection refused) while connecting to upstream, client连接到上游、客户端时连接()失败(111:连接被拒绝)
【发布时间】:2017-02-09 11:22:49
【问题描述】:

我正在尝试将 django 实例部署到 ec2 。我正在使用 nginx 和 gunicorn 的组合来实现这一点。我让 nginx istance 和 gunicorn 正确启动,并且我能够让我的实例运行。但是当我尝试将图像上传到我的应用程序的数据库时,我在 gunicorn error.log 中遇到了这个错误:

connect-failed-111-connection-refused-while-connecting-to-upstream

此外,我从前端到数据库的所有 api 调用都在控制台中返回 500 内部服务器。 我的 nginx.conf 看起来像

default_type        application/octet-stream;

# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
include /etc/nginx/sites-available/*;
index   index.html index.htm;

server {
    listen       127.0.0.1:80;
    listen       [::]:80 default_server;
    server_name  127.0.0.1;
    root         /usr/share/nginx/html;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
    }

    # redir

我的站点启用/默认文件为

upstream app_server_djangoapp {
server 127.0.0.1:8000 fail_timeout=0;

}

服务器{ #EC2 实例安全组必须配置为接受端口 80 上的 http 连接 听 80; server_name myec2isntance.com;

access_log  /var/log/nginx/guni-access.log;
error_log  /var/log/nginx/guni-error.log info;

keepalive_timeout 5;

# path for static files
location /static {
    alias xxxxxx;
}
location /media {
    alias xxxxxx;
}
location / {
 location / {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    if (!-f $request_filename) {
       proxy_pass http://app_server_djangoapp;
       break;
    }
}

}

我尝试了人们谈论的大部分内容 - 为文件夹添加正确的权限。将 localhost 更改为 127.0.0.1 等。我对这个主题比较陌生,所以非常感谢任何帮助!

谢谢

【问题讨论】:

    标签: django nginx amazon-ec2


    【解决方案1】:

    我建议将默认设置更改为:

    upstream app_server_djangoapp {
    server 127.0.0.1:8000 max_fails=3 fail_timeout=50;
    keepalive 512;
    }
    

    -删除

    keepalive_timeout 5;
    

    -为什么你有两个位置/块?

        location / {
        location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
    
        if (!-f $request_filename) {
           proxy_pass http://app_server_djangoapp;
           break;
        }
    }
    

    【讨论】:

    • 糟糕!没有注意到双位置块。我摆脱了它。现在我在日志中看不到任何内容,但每次我从前端进行 POST 调用时都会看到 500 内部服务器错误。
    • 这就是进步。运行 Django 的开发服务器时的跟踪是什么?此外,如果上传时间超过 30 秒,Gunicorn 将超时。您需要在 gunicorn 命令中使用 --timeout 120(2 分钟)
    猜你喜欢
    • 2015-04-03
    • 2014-02-26
    • 2012-11-21
    • 2020-03-13
    • 2013-11-03
    • 2019-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多