docker - Docker Compose 与 Express.js 和 Nginx - 502 响应
问题描述
我正在尝试使用 express.js 和 nginx 作为 Docker 的反向代理。express 应用程序可以单独与 docker 配合使用,但是当我添加 nginx 时,我得到 502 响应。它打印这个:
nginx_1 | 2019/03/30 21:14:04 [error] 6#6: *3 connect() failed (111: Connection refused) while connection to upstream, client: 172.20.0.1, server: localhost, request: "GET / HTTP /1.1”,上游:“ http://127.0.0.1:20000/ ”,主机:“localhost:21000”
docker-compose.yml
version: '3.3'
services:
app:
image: node:latest
volumes:
- ./app:/usr/src/service/app
working_dir: /usr/src/service/app
command: ["node", "app"]
ports:
- 20000:18000
nginx:
image: nginx:latest
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./nginx/error.log:/etc/nginx/error_log.log
- ./nginx/cache/:/etc/nginx/cache
ports:
- 21000:80
nginx.conf
events {
}
http {
upstream upstream_server {
server localhost:20000;
}
server {
listen 127.0.0.1;
listen 80;
server_name localhost;
location / {
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://upstream_server;
break;
}
}
}
解决方案
您的 nginx 容器和应用程序容器在同一个网络上,但不在同一个主机上 - 因此localhost
不是您可以找到您的应用程序运行的地方。
尝试app
作为server
上游的参数 - 这应该解析为app
compose 网络中容器的 IP。
推荐阅读
- api - 如何获取 Twilio API SID
- laravel - Laravel 通知中 Nexmo / Vonage 的自定义用户凭据
- flutter - 关于 Flutter 中使用 freezed 和 StateNotifier 进行状态管理的错误
- delphi - Firedac 数据流量
- python - 从具有“nan”值的字典中删除键
- mysql - MySQL:一个查询中的多个 SELECT 语句
- python - 在 django python 中,我想获取用户名以及下拉列表中的活动用户
- numpy - 在numpy数组中获取最大值及其索引
- logging - 您是否熟悉从整个日志文件中提取有用信息的日志分析工具?
- ios - 为什么 Microsoft Graph MSAL SDK 不支持 Microsoft 电子邮件(例如 ****@hotmail.com、****@live.com、****@outlook.com)帐户在 Onedrive 中上传?