【问题标题】:Docker compose error: nginx: [emerg] host not found in upstream "app" in /etc/nginx/conf.d/default.conf:21Docker compose 错误:nginx:在 /etc/nginx/conf.d/default.conf:21 的上游“app”中找不到 [emerg] 主机
【发布时间】:2020-11-09 23:45:15
【问题描述】:

尝试通过 docker compose 运行 nginx 和 web 应用程序

码头文件

FROM node:12.16.2 as build

RUN mkdir /app

COPY package*.json ./
RUN npm install

COPY . /app
RUN npm run-script build

COPY --from=build /app/build /var/www/roundmap.app

EXPOSE 3000

nginx 配置默认配置文件

server {
  listen 80;
  listen 443 ssl;
  listen 3000;
  server_name *.roundmap.app 185.146.157.206;

  root /var/www/roundmap.app;
  index index.html;

  ssl_certificate /etc/ssl/roundmap/roundmap.crt;
  ssl_certificate_key /etc/ssl/roundmap/roundmap.key;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_ciphers HIGH:!aNULL:!MD5;

  if ($scheme = http) {
    return 301 https://$server_name$request_uri;
  }

  location / {
    proxy_pass https://app:3000;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
  }
}

docker-compose.yml

version: "4"
services:
    app:
        build: roundmap/
        container_name: app
        ports:
            - 3000:3000
    nginx:
        image: nginx:1.17.2-alpine
        container_name: nginx
        ports:
            - 80:80
            - 443:443
        links:
            - app
        volumes:
            - ./default.conf:/etc/nginx/conf.d/default.conf
            - /etc/ssl/roundmap/roundmap.crt:/etc/ssl/roundmap/roundmap.crt
            - /etc/ssl/roundmap/roundmap.key:/etc/ssl/roundmap/roundmap.key

通过 docker-compose up 运行 并得到错误

nginx | 2020/07/20 16:39:19 [emerg] 1#1: 在 /etc/nginx/conf.d/default.conf:22 的上游“应用程序”中找不到主机 nginx | nginx:在 /etc/nginx/conf.d/default.conf:22 的上游“app”中找不到 [emerg] 主机 nginx 以代码 1 退出

请帮忙看看我哪里弄错了?

【问题讨论】:

  • This 可能会有所帮助。
  • 谢谢,但是我要为rest api添加另一个容器,它也会使用443端口,不可能在同一个端口上运行多个容器,所以需要代理

标签: docker nginx docker-compose


【解决方案1】:

在您的 default.conf 中,将 https://app:3000 替换为 http://app:3000,因为 SSL 终止发生在 Nginx 本身,应用程序仍在使用 http。

更新您的docker-compose.yaml

使用depends_on 代替links,它已被弃用。

version: "3.8"
services:
    app:
        build: roundmap/
        container_name: app
        command: [ "node", "app.js"]
        ports:
            - 3000:3000
    nginx:
        image: nginx:1.17.2-alpine
        container_name: nginx
        ports:
            - 80:80
            - 443:443
        depends_on:
            - app
        volumes:
            - ./default.conf:/etc/nginx/conf.d/default.conf
            - /etc/ssl/roundmap/roundmap.crt:/etc/ssl/roundmap/roundmap.crt
            - /etc/ssl/roundmap/roundmap.key:/etc/ssl/roundmap/roundmap.key

【讨论】:

  • 你在 docker-compose 中也缺少 command 来启动你的 nodejs 应用程序,例如:command: node server.js
猜你喜欢
  • 2020-11-08
  • 1970-01-01
  • 2016-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-22
  • 2021-06-14
  • 2016-02-11
相关资源
最近更新 更多