【发布时间】: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