首页 > 解决方案 > nginx:在 /etc/nginx/conf.d/default.conf 的上游“api:5000”中找不到 [emerg] 主机

问题描述

我已经搜索了几个小时,但找不到根本问题原因。这是我的 docker-compose.yml 文件,您可以在下面看到:

version: "3"
services:
   postgres:
     image: "postgres:latest"
     environment:
      - POSTGRES_PASSWORD=postgres_password
   redis:
     image: "redis:latest"
   nginx:
     restart: always
     build:
      dockerfile: Dockerfile
      context: ./nginx
     ports:
      - '3666:80'
     depends_on:
      - api
      - client
   api:
     build:
      dockerfile: Dockerfile.dev
      context: ./server
     volumes:
      - /app/node_modules
      - ./server:/app
     environment:
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - PGUSER=postgres
      - PGHOST=postgres
      - PGDATABASE=postgres
      - PGPASSWORD=postgres_password
      - PGPORT=5432
   client:
     stdin_open: true
     build:
      dockerfile: Dockerfile.dev
      context: ./client
     volumes:
      - /app/node_modules
      - ./client:/app
   worker:
     build:
      dockerfile: Dockerfile.dev
      context: ./worker
     volumes:
      - /app/node_modules
      - ./worker:/app

这是我的 nginx/default.conf

upstream client {
        server client:3000;
}

upstream api {
        server api:5000;
}

server {
        listen 80;

        location / {
                proxy_pass http://client;
        }

        location /api {
                rewrite /api/(.*) /$1 break;
                proxy_pass http://api
        }
}

当我尝试时:#docker-compose up --build
过程中出现此错误:

nginx_1 | nginx:在 /etc/nginx/conf.d/default.conf:6 的上游“api:5000”中找不到 [emerg] 主机

标签: dockernginxdocker-composedockerfile

解决方案


请检查您的 docker-compose 文件,您必须在 services 下设置 api


推荐阅读