连接到上游时,docker nginx 负载均衡器 connect() 失败(111:连接被拒绝)

发布于 2025-01-10 19:20:49 字数 1148 浏览 0 评论 0原文

我正在尝试使用 docker-compose & 来平衡具有 3 个实例的简单 Nodejs 应用程序的负载。 nginx。此配置适用于我的本地计算机(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 { }

docker-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"

我是 docker 新手。我很惊讶为什么这在本地有效但在 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"

I'm trying to load balance a simple Nodejs app with 3 instances using docker-compose & nginx. This configuration works on my local machine (windows laptop) but doesn't seem to work on EC2 server.

nginx.conf

http {

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

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

}

events { }

docker-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"

I'm new to docker. I'm surprised why this works locally but does not work on EC2 instance. The load balancer was able to resolve the url correctly but it still says connection refused.

Error:

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"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隔岸观火 2025-01-17 19:20:49

对于我来说,服务名称或 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/network

  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

For me service name or ip address not worked, only work put the gateway IP of network, for default bridge is 172.17.0.1.

In the servers put the (gateway ip):(port of container) and with this haproxy connects with success.

My example of custom network with fixed ips and gateway:

---- nginx config

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

----- haproxy config similar

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 / network

  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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文