nginx resolves an upstream hostname once at start-up and caches it forever, which breaks the moment a container is recreated with a new address. Using a variable changes the behaviour entirely.
resolver 127.0.0.11 valid=10s; # docker's embedded DNS
location /api/ {
set $upstream http://api:8080;
proxy_pass $upstream;
}
With a variable, nginx resolves per request and honours the TTL, so a restarted container is found again — at the cost of a lookup on every request, which the valid parameter caches. The resolver line is not optional in this form; without it nginx fails at start-up with “no resolver defined”. The trap: using a variable also stops nginx passing the URI through unchanged, so the location prefix has to be reattached explicitly.