HTTP 502 Bad Gateway
HTTP 502 Bad Gateway means a server acting as a gateway or proxy (Nginx, a load balancer, Cloudflare) got an invalid response, or no response, from the…
Quick answer
HTTP 502 Bad Gateway means a server acting as a gateway or proxy (Nginx, a load balancer, Cloudflare) got an invalid response, or no response, from the upstream server behind it. Usually the app server is down, crashing or listening on the wrong port.
Last updated · Maintained by FreeAIToolz · How we test
What does HTTP 502 Bad Gateway mean?
The proxy itself is working; the problem is between it and your application. In Nginx logs you will typically see “connect() failed (111: Connection refused)” or “upstream prematurely closed connection”.
| Status code | 502 |
|---|---|
| Name | Bad Gateway |
| Class | 5xx Server Error |
| Defined in | RFC 9110 §15.6.3 |
| Cacheable by default | No, unless Cache-Control says so |
| Safe to retry automatically? | Yes, with backoff |
What causes HTTP 502?
- The app process (Node, PHP-FPM, Gunicorn) is down or restarting.
- Wrong upstream address or port in the proxy config.
- The app crashed mid-response or returned malformed headers.
- Upstream responses with headers larger than the proxy buffers.
How do I fix HTTP 502?
- Check that the backend is running and listening on the configured port (systemctl status, pm2 status, docker ps).
- Read the proxy error log for the exact upstream error.
- Increase proxy buffer sizes if upstream headers are large (Nginx proxy_buffer_size).
What should a client do after HTTP 502?
Retry with backoff; 502s are often brief during deploys or restarts.
Does HTTP 502 affect SEO?
Brief 502s are harmless. Repeated 502s reduce Google’s crawl rate and can lead to pages being dropped.
Example 502 response
HTTP/1.1 502 Bad Gateway
Date: Mon, 01 Dec 2026 12:00:00 GMT
Content-Type: application/json
{"error":{"status":502,"message":"Bad Gateway"}} How to return 502 Bad Gateway in code
Express (Node.js)
res.status(502).json({ error: 'Bad Gateway' }); Next.js route handler
return Response.json({ error: 'Bad Gateway' }, { status: 502 }); FastAPI (Python)
raise HTTPException(status_code=502, detail="Bad Gateway") PHP
http_response_code(502); Nginx
location = /example {
return 502;
} Other 5xx status codes
The server failed to fulfil a valid request. Retrying later, with backoff, is usually reasonable.
- 500 Internal Server Error
- 501 Not Implemented
- 503 Service Unavailable
- 504 Gateway Timeout
- 505 HTTP Version Not Supported
- 507 Insufficient Storage
- 508 Loop Detected
- 511 Network Authentication Required
- 520 Web Server Returned an Unknown Error
- 521 Web Server Is Down
- 522 Connection Timed Out
- 523 Origin Is Unreachable
- 524 A Timeout Occurred
- 525 SSL Handshake Failed
- 526 Invalid SSL Certificate
Frequently asked questions
502 vs 504?
Why do I get 502 right after deploying?
Is 502 a client or server error?
Related
- 504 Gateway Timeout HTTP 504 Gateway Timeout means a gateway or proxy did not get a response from the upstream server in time.
- 503 Service Unavailable HTTP 503 Service Unavailable means the server is temporarily unable to handle the request, because it is overl.
- 500 Internal Server Error HTTP 500 Internal Server Error is a generic server-side failure: something went wrong in the application or se.
- 520 Web Server Returned an Unknown Error Cloudflare error 520 means the origin server returned an empty, unknown or unexpected response to Cloudflare.
- Open the HTTP Status Codes Reference → Free, no signup.