【问题标题】:docker nginx load balancer connect() failed (111: Connection refused) while connecting to upstreamdocker nginx 负载均衡器 connect() 在连接到上游时失败(111:连接被拒绝)
【发布时间】:2022-10-15 03:22:02
【问题描述】:

我正在尝试使用 docker-compose 和 nginx 对具有 3 个实例的简单 Nodejs 应用程序进行负载平衡。此配置适用于我的本地计算机(Windows 笔记本电脑),但似乎不适用于 EC2 服务器。

nginx.conf

http {

upstream all {
    server nodeapp1:4100;
    server nodeapp2:4200;
    server nodeapp3:4300;
}

server {
     listen 8080;
     location / {
          proxy_pass http://all/;
     }
}

}

events { }

码头工人-compose.yml

version: '3'

 services:
  lb:
  image: nginx
 volumes:
    - ./nginxproxy/nginx.conf:/etc/nginx/nginx.conf
 ports:
    - "3000:8080"
nodeapp1:
 image: nodeapp
 environment:
  - PORT=4100
 ports:
  - "4100:4100"
 nodeapp2:
  image: nodeapp
  environment:
  - PORT=4200
 ports:
  - "4200:4200"
 nodeapp3:
  image: nodeapp
  environment:
  - PORT=4300
  ports:
  - "4300:4300"

我是码头工人的新手。我很惊讶为什么这在本地有效但在 EC2 实例上无效。负载均衡器能够正确解析 url,但仍然显示连接被拒绝。

错误:

2022/02/28 20:00:22 [error] 33#33: *9 connect() failed (111: Connection refused) while 
 connecting to upstream, client: 62.113.237.40, server: , request: "GET / HTTP/1.1", 
 upstream: "http://172.121.0.5:4100/", host: "18.121.121.23:3000"

【问题讨论】:

  • 进一步分析,我认为根本原因是我的应用程序正在侦听 localhost,因为 NGINX 正在将请求转发到容器的内部 IP 地址。我认为解决方案是要么让 NGINX 转发到 EC2 服务器的本地主机(不是 NGINX 容器的本地主机),要么让应用程序监听 docker 容器内部 IP 地址的主机。

标签: docker nginx docker-compose nginx-reverse-proxy


【解决方案1】:

对我来说,服务名称或 IP 地址无效,只能输入网络的网关 IP,默认网桥为 172.17.0.1。

在服务器中放置(网关 ip):(容器端口)并使用此 haproxy 连接成功。

我的具有固定 ips 和网关的自定义网络示例:

---- nginx 配置

upstream loadbalancer {
  server 172.17.0.1:8001 weight=5;
  server 172.17.0.1:8002 weight=5;
}

----- haproxy 配置类似

backend be_pe_8545
    mode http
    balance     roundrobin

    server p1 172.20.0.254:18545 check inter 10s
    server p2 172.20.0.254:28545 check inter 10s

----- 泊坞窗应用程序/网络

  docker_app: ... 
    networks:
      public_network:
        ipv4_address: 172.20.0.50 

  public_network:
    name: public_network
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet:  172.20.0.0/24
          gateway: 172.20.0.254

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 2014-02-26
    • 2012-11-21
    • 2017-07-08
    • 2015-06-04
    • 2016-11-20
    相关资源
    最近更新 更多