Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Docker Nginx容器没有';除非重新启动容器,否则不要使用nginx.conf_Docker_Nginx_Docker Compose - Fatal编程技术网

Docker Nginx容器没有';除非重新启动容器,否则不要使用nginx.conf

Docker Nginx容器没有';除非重新启动容器,否则不要使用nginx.conf,docker,nginx,docker-compose,Docker,Nginx,Docker Compose,正如标题所说,除非重新启动nginx容器,否则它将无法正常工作。我在docker compose.yml文件中定义了几个服务,如下所示:reverseeproxy是我的nginx容器,service-a和service-b是Node.js服务器 version: "3.4" services: reverseproxy: container_name: reverseproxy build: context: ./proxy ports

正如标题所说,除非重新启动nginx容器,否则它将无法正常工作。我在
docker compose.yml
文件中定义了几个服务,如下所示:
reverseeproxy
是我的nginx容器,
service-a
service-b
是Node.js服务器

version: "3.4"
services:
  reverseproxy:
    container_name: reverseproxy
    build:
      context: ./proxy
    ports:
      - "80:80"
  service-a:
    container_name: service-a
    build:
      context: ./service-a
    ports:
      - "3500:3500"
    command: ["yarn", "run", "watch-debug"]
  service-b:
    container_name: service-b
    build:
      context: ./service-b
    ports:
      - "3501:3501"
    command: ["yarn", "run", "watch-debug"]
用于构建my
reverseproxy
服务的
Dockerfile
只需删除default.conf文件,然后将
nginx.conf
文件从我的主机复制到映像:

FROM nginx:alpine
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/nginx.conf
复制到图像中的我的
nginx.conf
文件如下所示:

worker_processes 1;

events { worker_connections 1024; }

http {

    sendfile on;

    server {
        listen 80;

        location /api/customers {
            proxy_pass         http://service-a:3500;
            proxy_redirect     off;
            proxy_set_header   Host $http_host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Host $server_name;
        }

        location /api/products {
            proxy_pass         http://service-b:3501;
            proxy_redirect     off;
            proxy_set_header   Host $http_host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Host $server_name;
        }
    }
}
当我
docker compose up
一切正常时,但当我发布到我的一个端点(例如,localhost:80/api/customers)时,nginx用502响应。但是如果我
docker容器停止reverseproxy
,然后
docker容器启动reverseproxy
,那么一切都会按预期进行,并且我能够使用localhost:80访问我的端点

我能够
docker exec-it reverseproxy/bin/sh
,并且能够验证
default.conf
是否消失,以及
nginx.conf
是否按预期从主机复制过来。我遵循了Docker Hub上nginx页面的示例配置,大多数在线教程显示了几乎相同的设置

这可能是什么原因造成的?如果不重新启动容器,如何使我的nginx revereproxy服务按预期工作


编辑:我正在使用邮递员发出请求localhost:80

@DavidMaze有正确的解决方案--谢谢

  reverseproxy:
    container_name: reverseproxy
    build:
      context: ./proxy
    ports:
      - "80:80"
    depends_on:
      - "service-a"
      - "service-b"

它只在重新启动的服务器上按预期工作是有意义的,因为那时其他服务都可用。刚刚试过,效果正如预期。

可能是一个愚蠢的问题,但是,当您可以用自己的文件覆盖default.conf时,为什么会进入一个复杂的扩展,即删除一个配置文件并添加一个新文件
COPY nginx.conf/etc/nginx/default.conf
如果您说nginx容器
依赖于:[service-a,service-b]
是否有帮助?nginx日志中除了502错误还有其他信息吗?@β.εηοιτ.βε根本不是一个愚蠢的问题。。。我遵循了nginx推荐的Dockerfile:Mhm,很有趣。。。从我在图像文档中看到的情况来看,他们更倾向于使用
/etc/nginx/templates/default.conf.template
,或者他们甚至完全覆盖/etc/nginx/nginx.conf