Common causes
- The application crashed, is restarting, or was never started.
- The proxy points at the wrong host or port, for example
proxy_pass http://127.0.0.1:3000while the app listens on 8080, or on a different network interface inside a container. - The application closed the connection mid-response, often because it ran out of memory and was killed.
- Response headers are larger than the proxy's buffer, which happens with very large cookies.
- A firewall or security group blocks traffic from the proxy to the application.
Fixing a 502 behind nginx
Start with the nginx error log, which states the exact reason:
sudo tail -n 50 /var/log/nginx/error.log
# connect() failed (111: Connection refused) while connecting to upstream
# → nothing listens on the upstream address: start the app or fix proxy_pass
# upstream prematurely closed connection while reading response header
# → the app crashed or closed the connection: check its logs
# upstream sent too big header while reading response header
# → raise proxy_buffer_size and proxy_buffersFixing a 502 on Cloudflare, AWS and other hosts
- Cloudflare: if the error page shows Cloudflare's branding, Cloudflare couldn't get a valid response from your origin server. Check that the origin is up and reachable on the expected port. Cloudflare's own 520 to 527 codes narrow the cause further.
- AWS Application Load Balancer: the target closed the connection or sent a malformed response. Check the target group health checks and make sure the app's keep-alive timeout is longer than the load balancer's idle timeout.
- Docker and Kubernetes: the app must listen on
0.0.0.0, not127.0.0.1, or it can't be reached from outside its container.
If you're visiting a site that shows 502
The problem is on the website's side. Reload after a minute, since many 502s happen during a deploy or restart. If it continues, the site's owners need to fix their server.
Frequently asked questions
What is the difference between 502 and 504?
Both come from a proxy. 502 means the upstream sent a bad response or refused the connection; 504 means it didn't answer in time.
What does "The proxy server received an invalid response from an upstream server" mean?
It's Apache's wording for a 502 when it runs as a reverse proxy with mod_proxy. The application behind it crashed, closed the connection or sent something that isn't valid HTTP. Look in Apache's error log for the proxy: line with the reason, then in the application's own logs.
What is the difference between 500 and 502?
A 500 comes from the application itself, which hit an error. A 502 comes from a proxy in front of the application, which couldn't get a usable response from it.
Can a 502 be cached?
Browsers don't normally cache a 502. CDNs may cache errors briefly if configured to, so purge the cache after fixing the origin if the error persists.
More http status code lookup guides
- 504 Gateway Timeout: Causes and Fixes
- 429 Too Many Requests: Meaning, Retry-After and Fixes
- 403 Forbidden: Meaning, Causes and How to Fix It
- 401 Unauthorized: What It Means and How to Fix It
- 500 Internal Server Error: Causes and How to Fix It
- 503 Service Unavailable: Causes and Fixes
- 400 Bad Request: Causes and Fixes (Including Cookie Too Large)
- 404 Not Found: What It Means and How to Fix It
- 301 Moved Permanently: Meaning, SEO and Examples
- 302 Found: Temporary Redirects Explained
- 408 Request Timeout: Causes and How to Fix It