在 Docker 容器内连接 Node.js 上游时,Nginx 报错“connection refused”。

5
我有以下设置:
Docker容器作为入口点具有supervisord, supervisord运行一个Node.js进程和Nginx代理。 Nginx端口在外部暴露。
在主机上,另一个Nginx用于SSL终止和vhost路由/负载平衡,作为Docker容器的代理使用。
我遇到了一些奇怪的问题,有时Docker容器内部的Nginx服务器报告连接到上游Node.js服务器的错误。看起来HTTP请求是200,所以即使只有一个上游,它也会自动重试?!
此外,Node.js进程的标准输出/标准错误中没有错误或崩溃的迹象。
这些是我看到的错误:
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 配置文件:

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

1
在这里找到了问题和解决方案 - http://serverfault.com/questions/529394/nginx-connect-failed-111-connection-refused-while-connecting-to-upstream - Gal Ben-Haim
9
把它作为答案发布并接受,这样人们更有可能看到解决方案。 - Jessie A. Morris
2个回答

0
Nginx连接到IPv6环回地址[::1]上的nodejs。nodejs可能只在IPv4上侦听。
尝试设置:
upstream api {
    server 127.0.0.1:5000;
}

-1
Nginx连接到IPv6回环[:: 1]上的nodejs。nodejs可能只在IPv4上侦听。 需要设置。
upstream api {
    server 127.0.0.1:5000;
}

1
我不明白。这个答案是在2017年3月30日发布的。你在2017年12月23日复制粘贴并回答了它?如果之前的答案是正确的,为什么不接受它呢? - Joshua Schlichting
那很尴尬。我为什么要这样做?!我不记得曾经做过这样的事情。实际上看一下问题评论的日期。我猜答案的日期出了问题。 - Gal Ben-Haim
这里引用了IPv6吗? - chovy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接