Cause 1 of 6
The upstream application crashed or never started.
Read the application's logs, not the proxy's: docker compose logs <service>, journalctl -u <service>, or the platform's log view. An app that exits at boot — a missing environment variable, a failed database connection — produces a 502 on every single request, which is the pattern to look for.
Cause 2 of 6
The proxy points at the wrong port or address.
Match the proxy_pass target, or the load balancer's target group, to the port the application actually listens on. An app bound to 127.0.0.1 is unreachable from another container or host — bind 0.0.0.0 when the proxy is elsewhere.
Cause 3 of 6
The upstream closes the connection mid-response.
nginx logs 'upstream prematurely closed connection'. That usually means a worker died during the request — an out-of-memory kill or an unhandled exception. The application log will show the crash; the proxy only saw the socket close.
Cause 4 of 6
Health checks are failing so no healthy targets remain.
Request the health path directly from inside the network and confirm it returns 200 within the configured timeout. A health endpoint that touches the database will fail the whole service the moment the database is slow, so keep it cheap.
Cause 5 of 6
A deploy is in progress and every instance is restarting at once.
Roll deploys instead of replacing all instances simultaneously, and give the platform a readiness check so traffic only arrives once an instance can serve it.
Cause 6 of 6
A large cookie or a long JWT can overflow nginx's default buffer, producing intermittent 502s for signed-in users only. Raising proxy_buffer_size and proxy_buffers fixes it; shrinking what you put in cookies fixes it better.