如何使用多个docker-compose.yml文件配置nginx? [英] How to configure nginx with multiple docker-compose.yml files?

查看:767
本文介绍了如何使用多个docker-compose.yml文件配置nginx?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果要使用Docker容器设置nginx,一种选择是在 docker-compose.yml 中设置nginx实例,并将nginx容器链接到所有应用程序容器。

If I want to setup nginx with my docker containers, one option is to setup the nginx instance in my docker-compose.yml, and link the nginx container to all application containers.

但是,此方法的缺点是 docker-compose.yml 变为服务器级,因为仅一个nginx容器可以将端口80/443暴露给互联网。

The drawback of this approach, however, is that the docker-compose.yml becomes server-level, since only one nginx container can expose ports 80/443 to the internet.

我对能够定义几个 docker-compose.yml 文件位于同一台服务器上,但是仍然可以通过单个服务器特定的nginx容器轻松地公开每个撰写文件中面向公众的容器。

I'm interested in being able to define several docker-compose.yml files on the same server, but still easily expose the public-facing containers in each compose file via a single server-specific nginx container.

我觉得这应该很容易,但是

I feel this should be pretty easy, but I haven't been able to find a good resource or example for this.

推荐答案

首先,您需要为此创建一个网络nginx和代理容器:

First, you need to create a network for nginx and the proxied containers:

docker network create nginx_network

接下来,在像这样的撰写文件中配置nginx容器此:

Next, configure the nginx container in a compose file like this:

services:
  nginx:
    image: your_nginx_image
    ports:
      - "80:80"
      - "443:443"
    networks:
      - nginx_network
networks:
  nginx_network:
    external: true

之后,您可以运行代理容器:

After that you can run proxied containers:

services:
  webapp1:
    image: ...
    container_name: mywebapp1
    networks:
      - nginx_network      # proxy and app must be in same network
      - webapp1_db_network # you can use additional networks for some stuff
  database:
    image: ...
    networks:
      - webapp1_db_network
networks:
  nginx_network:
    external: true
  webapp1_db_network: ~ # this network won't be accessible from outside

外部进行访问。要进行此工作,您需要正确配置nginx:

Also, to make this work you need to configure your nginx properly:

server {
    listen 80;
    server_name your_app.example.com;
    
    # Docker DNS
    resolver 127.0.0.11;
  
    location / {
        # hack to prevent nginx to resolve container's host on start up
        set $docker_host "mywebapp1";
        proxy_pass http://$docker_host:8080;
    }
}

您需要告诉nginx使用Docker的DNS,这样它才能

You need to tell nginx to use Docker's DNS, so it will be able to access containers by their names.

但是请注意,如果您先运行nginx容器,则nginx会尝试解析另一个容器的主机并失败,因为其他容器未运行然而。您可以将主机放入变量中使用技巧。

But note that if you run the nginx container before the others, then nginx will try to resolve another container's host and fail, because the other containers are not running yet. You can use a hack with placing the host into a variable. With this hack, nginx won't try to resolve host until receiving a request.

通过这种组合,nginx不会尝试解析主机。

With this combination you can have nginx always up, while starting and stopping proxied applications independently.

通过这种组合,您可以使nginx始终处于启动状态,同时独立启动和停止代理的应用程序。

更新:

Update:

如果要使用更动态的解决方案,可以按以下方式修改nginx配置:

If you want a more dynamic solution, you can modify the nginx config as follows:

server {
    listen 80;
    resolver 127.0.0.11;
    
    # define server_name with regexp which will read subdomain into variable
    server_name ~^(?<webapp>.+)\.example\.com;

    location / {
        # use variable from regexp to pass request to desired container
        proxy_pass http://$webapp:8080;
    }
}

使用此配置,将传递对webapp1.example.com的请求到容器 webapp1,webapp2.example.com到 webapp2等等。您只需要添加DNS记录并使用正确的名称运行应用程序容器即可。

With this configuration, a request to webapp1.example.com will be passed to container "webapp1", webapp2.example.com to "webapp2" etc. You only need to add DNS records and run app containers with right name.

这篇关于如何使用多个docker-compose.yml文件配置nginx?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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