G
G
Gennady S2021-07-17 11:03:01
Nginx
Gennady S, 2021-07-17 11:03:01

How to resolve hostname in Docker for nginx if container is not running?

I can't find a solution for a seemingly trivial situation. There are several application containers that are proxied by nginx - a classic scheme. In the solution, a situation is possible when certain applications will be idle disabled. In principle, this should also be a common situation (for example, when the application is under maintenance or under development). This is not about a specific time, it is clear that for a while you can keep waiting for nginx and use a backup server if the shutdown occurs while nginx is running. Here is an example of a conditional configuration:

upstream server_1 {
    server app_1:10;
}

upstream server_2 {
    server app_2:20;
    # можно задать разные опции, ожидание, количество попыток и даже backup-сервер, не принципиально
}

# ... еще некоторые приложения

server {
    # ... пропускаю типовую конфигурацию, она рабочая 

    location /server_1 {
        proxy_pass http://server_1;
        # ... заголовки и прочее
    }
    location /server_2 {
        proxy_pass http://server_2;
    }
    # ... еще сколько-то точек входа

    # можно попробовать и такой вариант, без upstream, итог не меняется:
    location /server_N {
        proxy_pass http://app_N:100;
    }
}


In any case, if we disabled, brought out for a while, for example, the application app_1 , nginx will crash with an error on startup or in the process:

host not found in upstream app_1

I found one hypothetical solution: use static IP addresses instead of Docker network names, say, set to app_1 is a well-defined ipv4_address: 172.18.0.2 , plus some maintenance service as an upstream backup, let's say another copy of nginx returning a 503 status:

upstream server_1 {
    server 172.18.0.2:10;
    server mainetnance:500 backup;
}


- but this is ugly and incorrect (for example, I suspect problems with scaling if you need to multiply app_1).

Are there any solutions? I think I'm missing something obvious.

Sergey Sokolov gives a solution in the case of proxy_pass with the definition of a variable in which the host will be stored, nginx, in this case, for some reason, does not check the value:

set $srv1 = http://app1:10;
...
proxy_pass $srv1;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Sokolov, 2021-07-17
@gscraft

try with a variable:

set $srv1 = http://app1:10;
...
proxy_pass $srv1;

Sorry for the design, it's inconvenient from a mobile phone.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question