Start free · Free tools · Fair usage limits
Browse Tools

Categories

Custom AI Solutions – AI2Flows
LIVE NEWS
Loading latest AI & SEO news…
Home›Tools›HTTP Status Codes Reference›HTTP 502 Bad Gateway
5xx Server Error

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 code502
NameBad Gateway
Class5xx Server Error
Defined inRFC 9110 §15.6.3
Cacheable by defaultNo, 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?

  1. Check that the backend is running and listening on the configured port (systemctl status, pm2 status, docker ps).
  2. Read the proxy error log for the exact upstream error.
  3. 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.

Frequently asked questions

502 vs 504?
502: the upstream returned an invalid response or refused the connection. 504: the upstream took too long to respond.
Why do I get 502 right after deploying?
The proxy sends traffic while the app is still restarting. Use health checks or zero-downtime deploys.
Is 502 a client or server error?
502 is in the 5xx class, a server error: the request may be fine, but the server could not complete it.