【问题标题】:Docker with Nginx: host not found in upstream带有 Nginx 的 Docker:在上游找不到主机
【发布时间】:2020-05-10 17:00:28
【问题描述】:

我正在尝试按照 this 指南为 docker 容器(提供静态文件)设置反向代理,使用带有 nginx 实例的另一个容器作为反向代理。

我希望在 / 上看到我的页面,但我在构建中被阻止并显示错误消息:

container_nginx_1  | 2020/05/10 16:54:12 [emerg] 1#1: host not found in upstream "container1:8001" in /etc/nginx/conf.d/sites-enabled/virtual.conf:2
container_nginx_1  | nginx: [emerg] host not found in upstream "container1:8001" in /etc/nginx/conf.d/sites-enabled/virtual.conf:2
nginx_docker_test_container_nginx_1 exited with code 1

我已经尝试了以下virtual.conf 文件的许多变体,这是当前的,基于给出的示例和其他各种页面:

upstream cont {
    server container1:8001;
}

server {
    listen        80;

    location / {
        proxy_pass http://cont/;
    }
}

如果您愿意查看第 3 方网站,我已经做了一个最小的 repo here,否则最相关的文件如下。

我的 docker-compose 文件如下所示:

version: '3'

services:
    container1:
        hostname: container1
        restart: always
        image: danjellz/http-server
        ports:
            - "8001:8001"
        volumes:
            - ./proj1:/public
        command: "http-server . -p 8001"
        depends_on:
            - container_nginx
        networks:
            - app-network

    container_nginx:
        build:
            context: .
            dockerfile: docker/Dockerfile_nginx
        ports:
            - 8080:8080
        networks:
            - app-network

networks:
    app-network:
        driver: bridge  

和 Dockerfile

# docker/Dockerfile_nginx

FROM nginx:latest

# add nginx config files to sites-available & sites-enabled
RUN mkdir /etc/nginx/conf.d/sites-available
RUN mkdir /etc/nginx/conf.d/sites-enabled
ADD projnginx/conf.d/sites-available/virtual.conf /etc/nginx/conf.d/sites-available/virtual.conf
RUN cp /etc/nginx/conf.d/sites-available/virtual.conf /etc/nginx/conf.d/sites-enabled/virtual.conf

# Replace the standard nginx conf
RUN sed -i 's|include /etc/nginx/conf.d/\*.conf;|include /etc/nginx/conf.d/sites-enabled/*.conf;|' /etc/nginx/nginx.conf

WORKDIR /

我正在使用 docker-compose up 运行它。

类似:react - docker host not found in upstream

【问题讨论】:

    标签: docker nginx docker-compose


    【解决方案1】:

    问题是如果主机名不能在upstream 块中解析,nginx 将无法启动。在这里,您已将服务 container1 定义为依赖于 container_nginx 。但是 nginx 容器永远不会启动,因为 container1 主机名没有被解析(因为 container1 还没有启动)你不认为它应该是反向的吗? Nginx 容器应该依赖于应用程序容器。

    此外,在您的 nginx 端口绑定中,您已经映射了 8080:8080,而在 nginx conf 中,您有 80 个监听。

    【讨论】:

      猜你喜欢
      • 2020-07-08
      • 2018-03-03
      • 2019-09-01
      • 2016-02-11
      • 2021-02-26
      • 1970-01-01
      • 2021-04-21
      • 2017-07-31
      • 2021-02-15
      相关资源
      最近更新 更多