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

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

  1. Home
  2. Blogs
  3. 502 Bad Gateway Error: What It Means and How to Fix It

502 Bad Gateway is one of the most common website errors: you open a page and get a blank screen saying "502 Bad Gateway". Let's look at what it means, who is to blame — the browser, the site or the server — and how to fix it: first for a regular visitor, then for a site owner running a VPS.

What a 502 error means

Code 502 means that an intermediary server (a gateway or proxy) received an invalid response from the server behind it. The typical setup: Nginx accepts the request, while the page itself is built by an application — PHP-FPM, Node.js, Python (Gunicorn, uWSGI) or a Docker container. If the application crashes, is overloaded or doesn't respond the way Nginx expects, the visitor sees a 502.

Important: 502 is a server-side error, not a problem with your computer. But the browser cache, DNS or extensions can sometimes make it look worse.

What the error looks like

  • 502 Bad Gateway with nginx at the bottom — the classic version;
  • HTTP Error 502 — Bad Gateway in Chrome and Edge;
  • a Cloudflare page "Bad gateway Error code 502" — the error is between Cloudflare and your server;
  • 502 Proxy Error — when Apache with mod_proxy sits in front of the site.

What to do as a visitor

  1. Reload the page after 30–60 seconds (Ctrl+F5 or Cmd+Shift+R). A 502 is often a short glitch while the server restarts.
  2. Open the site in incognito mode or another browser to rule out cache and extensions.
  3. Clear the cache and cookies for that site.
  4. Flush the DNS cache: on Windows — ipconfig /flushdns, on macOS — sudo dscacheutil -flushcache.
  5. Turn off your VPN or proxy and try again: sometimes the error happens on an intermediate node.
  6. Check whether the site works for others with a service like downforeveryoneorjustme.com. If it's down for everyone, the problem is on the owner's side.

Server-side causes of a 502

Cause How it shows up
The backend (PHP-FPM, Node.js, Gunicorn) crashed or isn't running 502 on every page at once
Out of memory, the process was killed by the OOM killer 502 under load, gone after a restart
PHP-FPM worker limit reached (pm.max_children) 502 at peak hours, "server reached pm.max_children" in the log
Wrong backend address in the Nginx config (port, socket) 502 right after changing settings
Response header buffers are too small "upstream sent too big header" in error.log
A firewall or SELinux blocks Nginx from reaching the backend "Permission denied" / "connection refused" in the log
Cloudflare can't reach the server Cloudflare page with code 502

How to fix 502 Bad Gateway: step by step for site owners

Step 1. Check the Nginx log

Connect to the server over SSH and open the error log — the cause is almost always there:

sudo tail -n 50 /var/log/nginx/error.log

Typical lines: connect() failed (111: Connection refused) while connecting to upstream — the backend isn't running; upstream prematurely closed connection — the backend crashed mid-response; upstream sent too big header — you need bigger buffers.

Step 2. Check that the backend is running

sudo systemctl status php8.3-fpm
sudo systemctl restart php8.3-fpm
# Node.js under pm2:
pm2 status
pm2 restart all
# Docker:
docker ps -a

If the service won't start, check its own log: journalctl -u php8.3-fpm -n 50.

Step 3. Verify the backend address in the config

In the site block, the address in fastcgi_pass or proxy_pass must match where the application actually listens:

# PHP-FPM via socket
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
# application on a port
proxy_pass http://127.0.0.1:3000;

After editing, test the syntax and reload Nginx: sudo nginx -t && sudo systemctl reload nginx.

Step 4. Check memory and load

free -h
dmesg -T | grep -i "killed process"
htop

If the OOM killer is terminating processes or free memory is constantly at zero, the site lacks resources. Options: lower pm.max_children, add swap, optimize database queries — or move to a VPS plan with more RAM.

Step 5. Increase buffers and timeouts if needed

fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
proxy_buffer_size 16k;
proxy_read_timeout 120s;

Step 6. If the site is behind Cloudflare

Check whether the site opens directly by the server's IP. If it works directly but not through Cloudflare, make sure the server firewall doesn't block Cloudflare's IP ranges and that the SSL mode in Cloudflare matches your server setup (Full / Full strict when a certificate is installed).

502 vs 500, 503, 504 and 524

Code Meaning Where to look
500 An error inside the application itself PHP / application logs
502 The gateway got an invalid response from the backend Nginx error.log, backend status
503 Service temporarily unavailable: overload or maintenance load, limits, maintenance mode
504 The backend didn't respond in time timeouts, slow queries
524 Cloudflare didn't get a response within 100 seconds long-running scripts, server load

How to prevent 502 errors

  • Enable automatic restarts: Restart=always in systemd, or pm2 for Node.js.
  • Set up uptime monitoring with Telegram alerts.
  • Keep a memory margin — at least 20–30% free RAM under normal load.
  • Test the config with nginx -t before every reload.
  • For busy projects, choose a server with a high CPU clock: PHP and Node.js handle each request in a single thread, so core frequency directly affects response time.

If 502s are caused by a lack of resources, the simplest fix is to move the site to a server with headroom: PSB Hosting VPS with NVMe storage and unmetered traffic, or Hi-CPU VPS on Ryzen 9 7950X for heavy sites.

FAQ

Is a 502 error my problem or the website's?

Almost always the website's. If the site doesn't open in another browser or on your phone over mobile data, all you can do is wait or contact the owner.

How long does a 502 usually last?

If it's a server restart, from a few seconds to a couple of minutes. If the application crashed and nobody restarted it, until the owner fixes it.

Does a 502 hurt SEO?

A short one doesn't. If search bots get a 502 for several days in a row, pages may drop out of the index — that's why monitoring matters.

How do I fix a 502 on WordPress?

Disable recently installed plugins (rename the plugin folder via SFTP), restart PHP-FPM and check the PHP memory limit. Usually it's a plugin or a shortage of PHP-FPM workers.

Related articles

ProxiesTrust.com: Multi-Country Proxies with Stable Performance

ProxiesTrust.com: Multi-Country Proxies with Stable Performance

Partner content. Prepared by the ProxiesTrust team. ProxiesTrust.com is a multi-country proxy service that partners value for its stability and performance. It is built for long-term use at a reasona

IpsTrust.com: Stable Proxies at Competitive Prices

IpsTrust.com: Stable Proxies at Competitive Prices

Partner content. Prepared by the IpsTrust team. Looking for reliable proxies for digital marketing, account management or system operations? IpsTrust.com provides proxy services built around a few si

TDS.ceo: a Tracker for Affiliate Marketing and Fast Traffic Launches

TDS.ceo: a Tracker for Affiliate Marketing and Fast Traffic Launches

Partner content. Prepared by the TDS.ceo team. When you launch traffic, you need to see what happens after the click: how many users reached the offer, where conversions came from, what the result co

FreePWA.com: Free PWAs for Gambling, Betting and Dating — and Your Own White-Label Service

FreePWA.com: Free PWAs for Gambling, Betting and Dating — and Your Own White-Label Service

Partner content. Prepared by the FreePWA team. In affiliate marketing you want to get to launch faster: pick an offer, prepare an app for the geo and send traffic. When every new idea means calculati

GetAntik: an Antidetect Browser with HollyProxy Proxies and TDS.ceo Statistics

GetAntik: an Antidetect Browser with HollyProxy Proxies and TDS.ceo Statistics

Partner content. Prepared by the GetAntik team. Working with multiple accounts quickly fills up with small tasks. One profile needs its proxy renewed, another has to be handed over to a colleague, an

ARNPROXY Review: Datacenter, ISP and Residential Proxies in One Dashboard

ARNPROXY Review: Datacenter, ISP and Residential Proxies in One Dashboard

If you run scrapers, SEO monitoring or automation on a server, the server is only half of the setup. The other half is the IP addresses your requests go out from. ARNPROXY is a proxy provider that cov

White Link Review: Stable White Pages

White Link Review: Stable White Pages

Hand-built white pages for Google, Meta, TikTok, Bing and other platforms — designed to pass moderation on the first try. White Link is a professional white page development service for media b

How to Run an Antidetect Browser on a VPS with Mobile Proxies

How to Run an Antidetect Browser on a VPS with Mobile Proxies

An antidetect browser only earns its keep when the machine behind it stays consistent and online. Run one on a laptop and it works until you close the lid, drop the connection, or need the same profil

VPS for Automation Scripts: Use Cases and Server Selection

VPS for Automation Scripts: Use Cases and Server Selection

Automation scripts help businesses handle repetitive tasks, process data, and connect different software systems. However, running these scripts on a personal computer can become inconvenient when tas

Telegram