Why this happens
A 502 comes from a gateway or proxy (Cloudflare, Nginx, a load balancer) that forwarded your request to the website's application server and got back an invalid response, or none at all.
The usual causes are an application server that crashed or isn't running, misconfigured upstream addresses, or a backend that closes connections abruptly. (If the proxy simply waits too long, you get 504 instead.)
Symptoms
- “502 Bad Gateway” from Nginx, Cloudflare, or a load balancer.
- It comes and goes, often under load or right after a deploy.
- Static files load from the CDN but app routes 502.
Common causes
- The upstream application process crashed or is not listening on the expected port/socket.
- The app is slow to start after a deploy and the proxy gave up.
- A mismatch between the proxy_pass target and where the app actually listens.
- Upstream keep-alive/timeout settings closing connections early.
Try these fixes
- Check that the upstream service is running and listening on the expected host:port or socket.
- Restart the application server first, then reload the proxy (command below); watch that the app is up before sending traffic.
- Review proxy error logs for “connect() failed,” “upstream prematurely closed,” or “no live upstreams” and adjust proxy_pass or timeouts accordingly.
Command
sudo systemctl restart your-app && sudo systemctl reload nginx
Replace your-app with the real service name. Check status with: systemctl status your-app --no-pager
Avoiding it in future
Add a readiness check before routing traffic post-deploy, and align proxy timeouts with real app response times.
