reverse proxy – Nginx: upstream server directive with regex


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?