gpt4 book ai didi

docker - Nginx在多容器docker compose设置中找不到上游主机,并且在client:3000上也找不到主机

转载 作者:行者123 更新时间:2023-12-02 21:32:30 24 4
gpt4 key购买 nike

这是github仓库:https://github.com/irahulsah/mutlicontainerapp
请访问更多信息,请帮助我修复此错误。
我正在本地用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',但是也可以在client:300错误上找到主机,关于如何解决此错误的任何想法,将不胜感激。
任何帮助或指示将不胜感激!!!

最佳答案

我今天有这个完全相同的问题。我通过将nginx上游引用的所有容器附加到同一docker虚拟网络中来解决此问题。
另外,请确保明确定义容器名称。如果我没有记错的话,docker-compose会在您的服务名称前加上yaml文件名。

  service_one:
networks:
- app-network # define the network
container_name: backend # the "backend" name must be used in the upstream directive in nginx configuration

关于docker - Nginx在多容器docker compose设置中找不到上游主机,并且在client:3000上也找不到主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64112684/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com