Nginx反向代理可访问多个Docker容器 [英] Multiple docker containers accessible by nginx reverse proxy

查看:1793
本文介绍了Nginx反向代理可访问多个Docker容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'd like to run multiple docker containers on one host VM which would be accessible through only one domain. I wanted to use request url to differentiate between containers. To achieve this I'm trying to set nginx server as reverse proxy and run it in the container also listening on port 80.

假设我有两个在端口3000和4000上运行的容器. 路由如下:

Let's say I have two containers running on port 3000 and 4000. The routing would be following:

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

问题是,即使试图为此类反向代理定义静态规则,我也正在堆叠. 无需任何位置即可正常运行:

The thing is I'm currently stack even with trying to define static rule for such reverse proxy. It works fine without any location:

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/

But when I add port location and try to access it using 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). 如果我检查了日志中对这些资源的请求:

It seems that first resource (main html) is requested correctly, but any other depending resource (for example js or css needed for this site) is missing. If I examine request for those resources I have in logs:

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/"

因此请求网址为 http://localhost:8455/public/css/fonts .min.css

代替 http://localhost:8455/3000/public/css /fonts.min.css

请问您有什么建议吗?这种情况可能吗?

Could I ask you for any suggestions ? Is this scenario possible ?

推荐答案

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

You can select a docker container per port, your example:

  • http://example.com:4000/css/fonts.min.css
  • http://example.com:3000/css/fonts.min.css

但是我更喜欢另一种方法,因为我认为更简单,可以通过域名访问docker容器,例如:

But there is another approach that I like more, because I think it is clearer, access to a docker container by domain name, e.g:

  • http://a.example.com/css/fonts.min.css
  • http://b.example.com/css/fonts.min.css

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

Whichever you choose, there is a project in github that helps you to implement docker multi-container reverse proxy: https://github.com/jwilder/nginx-proxy

针对以下情况,我使用docker-compose编写了一个示例:

I've written an example using docker-compose for a similar scenario at: http://carlosvin.github.io/posts/reverse-proxy-multidomain-docker/

这篇关于Nginx反向代理可访问多个Docker容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆