Clear framing of nginx as static server, reverse proxy, and load balancer in one compose stack. The ops habit that saves the most 502 tickets is resolving upstreams by compose service name on the shared network, not by published host ports — leave app ports internal and expose only 80/443 on the proxy service. After a backend recreate, a long-lived nginx worker can keep a stale resolved address unless you reload/recreate the proxy or use a variable-plus-resolver pattern, so treat upstream DNS churn as part of the deploy, not an afterthought.
For the load-balancer side I prefer least_conn once replicas are equal, keep max_fails and fail_timeout modest so a crashed container leaves the pool quickly, and enable upstream keepalive with protocol 1.1 plus an empty Connection header so sockets actually reuse. Always pass Host, X-Forwarded-For, and X-Forwarded-Proto so apps build public redirects correctly. After compose scale I hit each upstream by service name, then through nginx, and compare Set-Cookie and latency — mismatches almost always mean a stale upstream block or a missing forwarded header, not the balancing algorithm itself.