【发布时间】:2019-08-08 18:58:38
【问题描述】:
我正在使用 docker-compose 在本地运行一个多 docker 容器,这些容器是 React 前端“客户端”、一个 Nodejs 应用程序“api”,以及一个位于两个前面的 Nginx 代理。我一直在使用 docker-compose 设置,如下所示
version: '3'
services:
client:
build:
dockerfile: Dockerfile.dev
context: ./client
volumes:
- /usr/app/node_modules
- ./client:/usr/app
api:
build:
dockerfile: Dockerfile.dev
context: ./server
volumes:
- /usr/app/node_modules
- ./server:/usr/app
nginx:
restart: always
build:
dockerfile: Dockerfile.dev
context: ./nginx
ports:
- '8080:80'
我的 Nginx 设置如下
upstream client {
server client:3000;
}
upstream api {
server api:5000;
}
server {
listen 80;
server_name _;
location / {
if ($http_x_forwarded_proto != 'https') {
return 301 https://$host$request_uri;
}
proxy_pass http://client;
}
location /api {
if ($http_x_forwarded_proto != 'https') {
return 301 https://$host$request_uri;
}
rewrite /api/(.*) /$1 break;
proxy_pass http://api;
}
}
最近尝试启动容器时,出现以下错误:
nginx_1 | 2019/08/08 18:11:12 [emerg] 1#1: host not found in upstream "client:3000" in /etc/nginx/conf.d/default.conf:2
nginx_1 | nginx: [emerg] host not found in upstream "client:3000" in /etc/nginx/conf.d/default.conf:2
知道为什么 nginx 找不到上游吗?
我已尝试添加指向 nginx 设置块的链接,如下所示:
nginx:
restart: always
build:
dockerfile: Dockerfile.dev
context: ./nginx
links:
- client:client
- api:api
ports:
- '8080:80'
我还尝试了“depends_on”而不是链接。添加链接后,nginx不再抱怨并以代码0退出。但是当我访问localhost:8080时, 它提供了一个 301 重定向到https://localhost。
非常感谢任何帮助或指导!!!
【问题讨论】:
-
向您的 nginx 服务添加一个“depends_on”节,以确保您的上游服务在 nginx 服务启动之前正在运行/可访问,参考。 stackoverflow.com/a/56976398/1423507
标签: docker nginx docker-compose containers