502 Bad Gateway
Web/HTTP
High severity
HTTP 502 — Bad Gateway
A reverse proxy (Nginx, Cloudflare, a load balancer) got an invalid or empty response from the upstream app server.
Last reviewed 2026-08-29 by the Tech Issue Guide team.
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 — 3 steps
- 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.
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.
Frequently asked questions
Cloudflare shows 502 but my server looks fine — why?
Cloudflare could not get a valid response from your origin at that moment. Check origin logs for the same timestamps and confirm the origin is reachable on the port Cloudflare uses.