Nginx Reverse Proxy

nginx proxy dns

Nginx Reverse Proxy

DNS Caching Problem

Nginx resolves container names at startup and caches IPs. After CI/CD redeploy, container gets new IP -> nginx sends to old IP -> 502.

Fix

docker exec nginx nginx -s reload    # after each deploy

Better Fix (production)

resolver 127.0.0.11 valid=10s;
set $upstream my-container:8080;
proxy_pass http://$upstream;

Variables are resolved on each request, not at startup.

Basic Auth (inline, no htpasswd file)

# admin:password base64-encoded
if ($http_authorization != "Basic YWRtaW46cGFzc3dvcmQ=") {
    return 401;
}
add_header WWW-Authenticate 'Basic realm="Protected"' always;

Generate: echo -n 'user:pass' | base64

Updated: 2026-03-15 19:51:17