502 Bad Gateway: Meaning and Fixes

A 502 Bad Gateway means one server, acting as a gateway or proxy, asked another server for the page and got back an invalid answer or none. The fault sits behind the proxy: an app that crashed, stopped, or is listening on the wrong address. Visitors can only retry; owners must fix the upstream.

Where a 502 usually comes from
SetupWho shows the 502First check
Nginx in front of an appNginxIs the app running on the proxy_pass address?
Cloudflare in front of your serverCloudflare branded pageCan your origin be reached directly?
Vercel serverless functionVercel, code NO_RESPONSE_FROM_FUNCTIONFunction logs for a crash
WordPress on PHP-FPMNginx or the host's proxyIs PHP-FPM running and not out of workers?
AWS Application Load BalancerThe load balancerTarget health and app crash logs

Updated

The word gateway is the clue. Modern sites pass every request through at least one middle layer, such as a CDN, a load balancer or a web server acting as a reverse proxy. A 502 is that layer reporting that the next server along broke the conversation. So the useful question is which hop failed, not what is wrong with the browser.

Find the failing hop

Ask the upstream directly from the proxy machine

If this fails too, the app is the problem; if it works, look at the proxy's settings.

curl -i http://127.0.0.1:3000/health
sudo tail -n 50 /var/log/nginx/error.log

Fixes by platform

Nginx: start the app under a process manager that restarts it, make proxy_pass match the app's listen address, and raise proxy_buffer_size only if the log mentions headers too big. Our Nginx guide covers each log line in detail.

Vercel: NO_RESPONSE_FROM_FUNCTION means the function exited or crashed without sending a response. Read the function logs for the request and make sure every code path returns or sends a response.

WordPress: on Nginx with PHP-FPM, a 502 often means PHP-FPM is down or every worker is busy. Restart it, check its log for 'server reached pm.max_children', and disable a plugin that has just been updated if errors began after an update.

Load balancers and containers

An AWS Application Load Balancer returns 502 when a target closes the connection or sends a malformed response, and it shows unhealthy targets in the console. In Kubernetes or Docker, a container that restarts on a crash loop gives intermittent 502s until it stays up.

On a site hosted by We.Inc there is no web server, PHP process or function timeout of your own to tune. If a page on a We.Inc site returns this code, send support the address and the time so we can check our side.

How it works

  1. Reload once after a few seconds; a deploy or restart can cause a brief 502.
  2. Note who branded the error page: Cloudflare, Nginx, your host or your platform.
  3. Open that layer's logs and find the matching request and its upstream error.
  4. Fix the upstream app or address, then confirm with curl from the proxy machine.

Tips

  • A 502 on every page at once points at a stopped app or a wrong address. A 502 on one page points at code that crashes for that route.
  • Deploys that restart the app without a health check produce short bursts of 502s. Rolling restarts or a readiness check remove them.
  • Response headers that are too large for the proxy's buffer, often from big cookies, cause 502s that only some users see.
  • Visitors: clearing the cache rarely helps a 502, but trying again in a minute, or on another network, can tell you whether the site is down for everyone.

Frequently asked questions

Is a 502 Bad Gateway my fault?

If you are visiting the site, no. The error happened between the site's own servers. You can reload or come back later. If you run the site, the problem is in your app or your proxy settings.

What is the difference between 502 and 504?

A 502 means the upstream server answered with something invalid or dropped the connection. A 504 means the proxy waited for an answer and none came in time. The first suggests a crash, the second slowness.

How do I fix 502 Bad Gateway in Nginx?

Check that the app behind Nginx is running and that proxy_pass points at the exact host, port or socket it listens on. The Nginx error log gives the precise reason, such as connection refused or upstream prematurely closed connection.

Why does Cloudflare show 502 Bad Gateway?

If the error page carries Cloudflare's branding, Cloudflare could not get a valid response from your origin server. Check that the origin is up and that its firewall allows Cloudflare's IP ranges. An unbranded page means your origin itself sent the 502.

Does a 502 error hurt SEO?

A short one does not. Google treats 5xx errors as temporary and retries, but if they persist, crawling slows and pages can drop from results. Fix lasting 502s quickly.

Related: Nginx 502 and other Nginx errors, 504 Gateway Timeout, 503 Service Unavailable, HTTP status codes.

Start for free or see all free tools. We.Inc is free to start, with no card needed. AI building is on paid plans from $20 a month.

More in Guides

Product

Who It's For

Features

Resources

Company

View Sitemap