How to configure nginx with multiple docker-compose.yml files? How to configure nginx with multiple docker-compose.yml files? nginx nginx

How to configure nginx with multiple docker-compose.yml files?


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

docker network create nginx_network

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

services:  nginx:    image: your_nginx_image    ports:      - "80:80"      - "443:443"    networks:      - nginx_networknetworks:  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_networknetworks:  nginx_network:    external: true  webapp1_db_network: ~ # this network won't be accessible from outside

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;    }}

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

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.

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

Update:

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;    }}

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.