【问题标题】:Nginx connection refused while connecting to Node.js upstream inside a Docker containerNginx 连接在 Docker 容器内连接到上游的 Node.js 时被拒绝
【发布时间】:2014-09-23 10:40:32
【问题描述】:

我有以下设置:

以 supervisord 作为入口点的 Docker 容器, supervisord 运行一个 Node.js 进程和 Nginx 代理。 Nginx 端口暴露在外面。

在主机上,另一个 Nginx 用于 SSL 终止和 vhost 路由/负载平衡,它用作 Docker 容器的代理。

我遇到了一些奇怪的问题,有时 Docker 容器内的 Nginx 服务器在连接到上游 Node.js 服务器时报告错误。似乎 HTTP 请求是 200,所以即使上游只有一个,它也可能会自动重试?!

此外,Node.js 进程 stdout/stderr 中没有任何错误或崩溃的迹象。

这是我看到的错误:

2014/07/31 12:48:54 [error] 15#0: *10 connect() failed (111: Connection refused) while connecting to upstream, client: 172.17.42.1, server: f074d2f4389f, request: "GET /users/me HTTP/1.1", upstream: "http://[::1]:3000/users/me", host: "xxx.xxx.com", referrer: "https://xxx.xxx.com/"

可能是什么问题?

这是 supervisord 的配置文件:

[supervisord]
nodaemon=true
logfile=/var/log/supervisor/supervisord.log    ; supervisord log file
logfile_maxbytes=50MB                           ; maximum size of logfile before rotation
logfile_backups=10                              ; number of backed up logfiles
loglevel=info                                  ; info, debug, warn, trace
pidfile=/var/run/supervisord.pid                ; pidfile location
childlogdir=/var/log/supervisor/               ; where child log files will live"

并且该文件包含在其他文件中:

[include]
files = /etc/supervisor/conf.d/supervisord.conf

[program:api]
command=node api-cluster.js
directory=/src
autorestart=true
startretries=100000000
stdout_logfile=/var/log/supervisor/api_stdout.log
stderr_logfile=/var/log/supervisor/api_stderr.log

[program:nginx]
command=/usr/sbin/nginx
autorestart=true
startretries=100000000
stdout_logfile=/var/log/supervisor/nginx_stdout.log
stderr_logfile=/var/log/supervisor/nginx_stderr.log"

这是 Docker 容器内的 Nginx conf 文件:

# -------------------------------------------------------------------
#                         Nginx configuraiton
# -------------------------------------------------------------------

worker_processes 4;
daemon off;

error_log   stderr  info;
pid         /var/run/nginx.pid;

events {
    #use epoll;
    worker_connections 768;
}

http {
    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

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

    log_format main 'ip=\$http_x_real_ip [\$time_local] '
    '"\$request" \$status \$body_bytes_sent "\$http_referer" '
    '"\$http_user_agent"' ;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    upstream app_proxy {
        server localhost:3000;      # nodejs server
    }

    server {
        listen          80 deferred;
        server_name     \$hostname;
        access_log      /dev/stdout main;

        location ~ ^/(images/|img/|javascript/|js/|css/|stylesheets/|flash/|media/|static/|scripts/|views/|styles/|bower_components/|robots.txt|humans.txt|favicon.ico|home/|html|xml) {
            root /src/web/public;
            access_log off;
            expires max;
        }

        location / {
            proxy_redirect off;
            #proxy_set_header X-Real-IP \$remote_addr;
            #proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto \$scheme;
            proxy_set_header Host \$http_host;
            proxy_set_header X-NginX-Proxy true;
            proxy_intercept_errors off;
            proxy_pass       http://app_proxy/;
        }
    }
}

【问题讨论】:

标签: node.js nginx docker supervisord


【解决方案1】:

Nginx 在 IPv6 环回 [::1] 上连接到 nodejs。 nodejs 可能只是在监听 IPv4。

尝试设置

upstream api {
    server 127.0.0.1:5000;
}

【讨论】:

    【解决方案2】:

    Nginx 在 IPv6 环回 [::1] 上连接到 nodejs。 nodejs 可能只是在监听 IPv4。

    需要设置

    upstream api {
        server 127.0.0.1:5000;
    }
    

    【讨论】:

    • 我不明白。这个答案是2017年3月30日发布的,你复制粘贴在2017年12月23日再回答?如果它是正确的,为什么不接受你几个月前发布的答案?
    • 这很尴尬。我为什么要这么做?!我不记得曾经做过这样的事情。实际查看问题的 cmets 日期。我认为答案的日期有问题。
    • 这里引用的 ipv6 在哪里?
    猜你喜欢
    • 1970-01-01
    • 2019-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多