繁体   English   中英

nginx反向代理可访问的多个docker容器

[英]Multiple docker containers accessible by nginx reverse proxy

我想在一个主机VM上运行多个docker容器,只能通过一个域访问它。 我想使用请求网址来区分容器。 为了实现这一点,我试图将nginx服务器设置为反向代理,并在容器中运行它,同时监听端口80。

假设我有两个容器在端口3000和4000上运行。路由将遵循:

docker-host.example.com/3000 -> this will access container exposing port 3000
docker-host.example.com/4000 -> this will access container exposing port 4000

事情是,我正在堆叠,甚至试图为这种反向代理定义静态规则。 没有任何位置它工作正常:

upstream application {
    server <docker container>:3000;
}

server {
        listen 80;

        location / {
            proxy_pass_header  Server;
            proxy_set_header   Host $http_host;
            proxy_redirect     off;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Scheme $scheme;
            proxy_pass         http://application/;
        }
}

但是当我添加端口位置并尝试使用localhost访问它时:{nginx port} / 3000 /

upstream application {
    server <docker container>:3000;
}

server {
        listen 80;

        location /3000/ {
            proxy_pass_header  Server;
            proxy_set_header   Host $http_host;
            proxy_redirect     off;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Scheme $scheme;
            proxy_pass         http://application/3000/;
        }
}

似乎第一个资源(主html)被正确请求,但缺少任何其他依赖资源(例如,此站点所需的js或css)。 如果我检查对日志中的资源的请求:

09:19:20 [error] 5#5: *1 open() "/etc/nginx/html/public/css/fonts.min.css" failed (2: No such file or directory), client: 172.17.0.1, server: , request: "GET /public/css/fonts.min.css HTTP/1.1", host: "localhost:8455", referrer:"http://localhost:8455/3000/"

所以请求url是http:// localhost:8455 / public / css / fonts.min.css

而不是http:// localhost:8455/3000 / public / css / fonts.min.css

我能问你一些建议吗? 这种情况可能吗?

您可以为每个端口选择一个docker容器,例如:

但是我还有另外一种方法,因为我认为通过域名访问docker容器更清楚,例如:

无论你选择哪个,github中都有一个项目可以帮助你实现docker多容器反向代理: https//github.com/jwilder/nginx-proxy

我在一个类似的场景中使用docker-compose编写了一个例子: http//carlosvin.github.io/posts/reverse-proxy-multidomain-docker/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM