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

k2fxgqgv  于 2022-11-02  发布在  Nginx
关注(0)|答案(1)|浏览(221)

我正在尝试使用docker-compose & nginx对一个简单的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 { }

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

dojqjjoe1#

对于我的服务名称或ip地址不工作,只工作把网关IP的网络,为默认网桥是172.17.0.1。
在服务器中放入(网关ip):(容器端口),并与此haproxy连接成功。
我的自定义网络的例子与固定ip和网关:
---- 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应用程序/网络

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

相关问题