本文介绍了如何使用多个 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

接下来,像这样在 compose 文件中配置 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 将尝试解析另一个容器的主机并失败,因为其他容器尚未运行.您可以使用 hack 将主机放入变量中.有了这个 hack,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配置如下:

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 22:36