【问题标题】:Nginx can't find upstream host in multi-container docker compose setup and also host can't be found on client:3000Nginx 在多容器 docker compose 设置中找不到上游主机,在客户端也找不到主机:3000
【发布时间】:2020-12-14 00:22:53
【问题描述】:

这里是 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”,但也得到了主机可以在客户端找到:300 错误,任何关于如何解决此错误的想法,将不胜感激。

非常感谢任何帮助或指导!!!

【问题讨论】:

  • nginx/Dockerfile.dev 中有什么内容?你是如何启动整个堆栈的?后端服务是否启动成功?
  • in nginx/Dockerfile.dev -> 下面的代码来自 nginx COPY ./default.conf /etc/nginx/conf.d/default.conf ,是的,后端服务器已成功启动,只有这个客户端在主机 3000 上找不到客户端,这种类型的错误即将到来......
  • 更多详情:github repo链接:github.com/irahulsah/mutlicontainerapp,请帮帮我,我不知道该怎么办,我卡住了
  • 我正在使用 docker-compose.yml 文件启动整个堆栈 -> sudo docker-compose up
  • 为什么不使用 docker swarm 进行复制?

标签: docker docker-compose dockerfile


【解决方案1】:

我今天遇到了同样的问题。我通过在同一个 docker 虚拟网络中附加 nginx 上游引用的所有容器来解决它。

另外,请确保明确定义容器名称。如果我没记错的话,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

【讨论】:

  • 哇 - 谢谢这个。我有三个容器,反向代理将它们暴露为不同的主机名,并且忘记将其中一个放入同一个网络。这个答案有助于找到这一点。
猜你喜欢
  • 1970-01-01
  • 2021-02-26
  • 2019-09-01
  • 2020-07-08
  • 2018-03-03
  • 1970-01-01
  • 2020-06-15
  • 2021-04-21
  • 2018-02-06
相关资源
最近更新 更多