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.

What causes 502 Bad Gateway

  • 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.

How to fix it

  1. Check that the upstream service is running and listening on the expected host:port or socket.
  2. Restart the application server first, then reload the proxy (command below); watch that the app is up before sending traffic.
  3. Review proxy error logs for “connect() failed,” “upstream prematurely closed,” or “no live upstreams” and adjust proxy_pass or timeouts accordingly.

Terminal / CLI 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

How to confirm the fix: App routes return 200 consistently, including under a short load test.

Prevent it from coming back

Add a readiness check before routing traffic post-deploy, and align proxy timeouts with real app response times.