0

I've got a docker-based architecture: A configurable number of containers being load balanced by one nginx container. All of these containers exist on the same local network, backend (created by docker-compose).

The containers I want to load balance will always listen on port 77894, and will always have a name of the form model_* (where the * could be any positive integer). The nginx.conf in the single nginx container needs to be configured correctly.

This nginx.conf works fine, but I have to specify each replica of model explicitly in the upstream service:

http {

    upstream models {
        server model_1:77894;
        server model_2:77894;
        server model_3:77894;
    }

    server {
        listen 80;

        location / {
            proxy_pass http://models;
        }
    }
}


events { }

This is not dynamic; I would have to know the number of replicas of model_* beforehand.

When I try this (and other variants):

upstream {
    server ~ ^model_[0-9]:77894
}

nginx throws an error: invalid parameter "^model_[0-9]:77894" in /etc/nginx/nginx.conf:4

Is there any way to tell nginx to reverse proxy to all containers with names like model_*:77894?

shinvu
  • 103
  • 2

1 Answers1

2

The server directive supports only explicit upstream IP address:port pairs or Unix domain socket paths.

You should use a configuration management system, which updates nginx configuration file and sets up containers at the same time, therefore always keeping nginx configuration in sync.

One option is to add all possible server entries in the upstream section and let nginx detect if backend is available or not. If nginx tries to use non-existing backends too often, fail_timeout value can be increased from the default 10 second value.

Tero Kilkanen
  • 34,499
  • 3
  • 38
  • 58