All posts

Blog

502 Bad Gateway Error: What It Means and How to Fix It

Illustrated cover for the Troubleshooting guide "502 Bad Gateway Error: What It Means and How to Fix It"

A 502 Bad Gateway error usually appears at the worst possible moment, right after a launch, during a sale, or just as you share a link with a client. Unlike some errors, it isn't about your browser or a missing page. It's about two servers failing to talk to each other properly.

Once you understand how the pieces connect, a 502 becomes much easier to diagnose. This guide explains what's going on behind the scenes, what visitors can try, and how site owners can track down and fix the cause.

What does 502 Bad Gateway mean?

Modern websites rarely run on one program. A typical setup looks something like this:

  1. A visitor's browser sends a request.
  2. A front-facing server receives it. This could be a CDN like Cloudflare, a load balancer, or a web server like Nginx acting as a reverse proxy.
  3. That front server passes the request to a back-end service, such as PHP-FPM, a Node.js app, a Python app server like Gunicorn, or another web server.
  4. The back end builds the page and sends it back up the chain.

A 502 Bad Gateway means the front server (the "gateway" or proxy) contacted the back end and got an invalid response, or no usable response at all. The front server is working fine. It's the thing behind it that failed.

You might see it worded in different ways:

  • "502 Bad Gateway"
  • "502 Bad Gateway nginx"
  • "HTTP Error 502"
  • "Error 502" on a Cloudflare-branded page
  • "502 Proxy Error"

Quick fixes for visitors

If you're simply trying to visit a site, there's not much you can break, but a few things are worth trying:

  • Wait a minute and reload. Many 502s are brief, caused by a service restarting or a short traffic spike.
  • Try another browser or a private window. This rules out a strange cached response.
  • Check whether it's just you. Try the site on mobile data or ask someone else to load it.
  • Clear your DNS cache or restart your router if only some sites fail. This is rare, but it can help when you've been routed to a stale address.

If the site stays down everywhere, the problem is on the server side and only the owner or host can fix it.

Why 502 errors happen (for site owners)

On the server side, 502s almost always fall into one of these buckets.

The back-end service is down or crashed

The most common cause by far. If PHP-FPM, your Node app or your app server has stopped, the proxy has nothing to talk to. This can happen after a crash, a failed update, a server reboot where the service didn't start automatically, or the system killing a process because it ran out of memory.

The back end is overloaded

If every PHP worker is busy, new requests have to wait. When they can't be accepted, the proxy may return a 502 (or a 504 if it waits too long). Traffic spikes, slow database queries and bots crawling heavily can all exhaust workers.

Wrong socket or port in the configuration

Nginx needs to know exactly where to send requests, such as unix:/run/php/php8.2-fpm.sock or 127.0.0.1:3000. If you upgraded PHP and the socket path changed, or your app now listens on a different port, Nginx will knock on a door that doesn't exist.

Permission problems on the socket

If PHP-FPM creates its socket file with permissions that the Nginx user can't access, every request fails with a 502 and a "permission denied" message in the log.

Firewall or CDN issues

If you use a CDN or proxy, it needs to reach your origin server. A firewall rule that blocks the CDN's IP ranges, an origin server IP that changed, or an SSL mismatch between the CDN and your server can all produce a 502 on the CDN's error page.

Response headers that are too large

Some apps send very large headers or cookies. If they exceed Nginx's buffer sizes, Nginx may reject the response and log an "upstream sent too big header" error.

How to fix a 502 Bad Gateway step by step

1. Check the error log first

The proxy's log almost always says exactly what went wrong. On a VPS, look at /var/log/nginx/error.log. Key phrases to watch for:

  • "connect() failed (111: Connection refused)": the back end isn't running or isn't listening where Nginx expects.
  • "connect() to unix:/run/php/... failed (2: No such file or directory)": the socket path is wrong or PHP-FPM is stopped.
  • "(13: Permission denied)": a socket permission issue.
  • "upstream prematurely closed connection": the back end crashed mid-request.
  • "upstream sent too big header": buffer sizes need adjusting.

On shared hosting, check the error log in your control panel, and if it shows nothing useful, contact support with the time of the error.

2. Restart the back-end service

On a VPS, check the status and restart if needed. For PHP-FPM on Ubuntu, for example:

sudo systemctl status php8.2-fpm
sudo systemctl restart php8.2-fpm

For a Node.js app managed by PM2, pm2 status and pm2 restart all are a good start. For a Gunicorn service, use systemctl status with your service name. If the service fails to start, the status output or journalctl -u servicename will usually explain why.

Also make sure the service is enabled to start on boot, so a reboot doesn't bring the error back.

3. Confirm the socket or port matches

Compare the fastcgi_pass or proxy_pass line in your Nginx site config with where the back end is really listening. Check the PHP-FPM pool file (often in /etc/php/<version>/fpm/pool.d/www.conf) for the listen value, or check your app's settings for its port. After any change, test and reload Nginx:

sudo nginx -t
sudo systemctl reload nginx

4. Look at memory and resource use

Run free -m and top or htop to see whether the server is out of memory. If you find "Out of memory: Killed process" in dmesg or the system journal, the kernel is killing your app. Solutions include tuning the number of PHP-FPM workers (pm.max_children) to fit your RAM, adding swap as a safety net, fixing memory-hungry code or plugins, or upgrading to a plan with more memory.

5. Check your CDN and firewall

If the error page is branded by your CDN, test your origin directly. You can temporarily pause the CDN, or use a hosts file entry pointing your domain at the origin IP. If the origin works, the problem is between the CDN and your server. Make sure your firewall allows the CDN's published IP ranges and that the SSL mode in the CDN matches what your server supports.

6. Disable recently added plugins or code

If a single page or action triggers the 502, a plugin or recent code change may be crashing PHP. Roll back the change, or disable plugins one at a time to find the troublemaker.

Preventing future 502 errors

  • Monitor uptime so you know about outages before your visitors do.
  • Keep PHP, your app runtime and plugins updated, but test updates first.
  • Size worker pools to your server's real memory, not to defaults.
  • Use caching to reduce the number of requests that hit PHP at all.
  • Set services to restart automatically if they crash.

The bottom line

A 502 Bad Gateway means a proxy couldn't get a valid answer from the service behind it. In most cases that service is stopped, overloaded or misconfigured. Start with the proxy's error log, restart the back end, confirm the socket or port, and check memory. Work through those in order and you'll usually find the cause in minutes, not hours.