What does HTTP 502 mean?
A gateway or proxy got an invalid response from the upstream server. You see 502 from Nginx, Cloudflare, load balancers and CDNs when the application server behind them crashed, refused the connection or returned something that is not valid HTTP.
5xx Server error: The request looked valid, but the server failed to fulfil it. The fix is on the server side.
Common causes of a 502 error
- The application process crashed or is not running (PHP-FPM, Node, Gunicorn).
- Wrong upstream port or socket in the proxy config.
- Firewall blocking the proxy from the origin.
- Origin response headers too large for the proxy buffers.
How to fix 502 Bad Gateway
- Check that the app server is running and listening on the configured port.
- Read the proxy’s error log (for Nginx:
connect() failedorupstream prematurely closed). - Restart the application and check its own logs.
How to find the cause of a 502
A 502 means the proxy reached your app but got a bad answer, or nothing, so check the app first.
sudo systemctl status your-app
sudo tail -n 30 /var/log/nginx/error.log
curl -I http://127.0.0.1:3000/If connect() failed (111: Connection refused) appears in the log, the app is down or listening on another port. If the local curl works, the proxy’s upstream setting is wrong. Behind Cloudflare, the error page shows whether your server or Cloudflare is failing.
502 and SEO
Like 500 — short incidents are fine, ongoing 502s reduce crawling and indexing.
Example response
HTTP/1.1 502 Bad Gateway
How to return a 502 response
Node.js (Express)
res.status(502).json({ error: 'Bad Gateway' });PHP
http_response_code(502);Python (Flask)
return {'error': 'Bad Gateway'}, 502Go (net/http)
w.WriteHeader(502)nginx
return 502;
Related status codes
- 503 Service Unavailable — The server is temporarily unable to handle the request.
- 504 Gateway Timeout — A gateway or proxy did not get a response from the upstream server in time.
- 500 Internal Server Error — The server hit an unexpected condition and could not complete the request.
- 520 Web Server Returned an Unknown Error — Cloudflare received an empty, unknown or unexpected response from the origin server.
Check which status code a URL returns with the HTTP header checker.
Pertanyaan yang sering diajukan
What does status code 502 mean?
How do I fix a 502 error?
connect() failed or upstream prematurely closed).