HTTP error 502
502 Bad Gateway: what it means and how to fix it
The short answer
The front door answered, but the room behind it did not. Your web server or CDN asked PHP or your application for a page and got nothing usable back.
- Backup before every change
- Fixed price before we start
- Remote, anywhere in the world
- Free diagnosis
Every 502 involves two parties: the gateway that shows you the error and the upstream that failed. The first job is to find out which link broke, and the gateway’s error log usually tells you. “connect() failed (111: Connection refused) while connecting to upstream” points to a stopped PHP process. “upstream prematurely closed connection” means it died halfway through a request.
Sound familiar?
Signs of a 502 Bad Gateway error
- A page that says “502 Bad Gateway”
- Apache’s wording: “The proxy server received an invalid response from an upstream server”
- A Cloudflare-branded page with “Bad gateway, Error code 502”
- The error comes and goes: gone after a reload, back at busy moments
- It began after a deployment, a server restart, a migration or a traffic peak
Causes
Why a 502 Bad Gateway appears
The application process crashed or stopped
PHP-FPM (the process that runs PHP for nginx), Node or Gunicorn stopped or restarted. The web server then has nothing to talk to. This is the most common cause.
A request that was too heavy
A script that eats all its memory, or hits a process limit, gets killed mid-request. The gateway sees a broken answer and reports a 502.
Proxy or CDN cannot reach your server
Cloudflare or a load balancer points at the wrong origin IP, for example after a migration, or your server is not accepting its connections.
Server overloaded
When every PHP worker is busy, new requests fail. PHP-FPM logs “server reached pm.max_children setting” when this happens.
A faulty plugin, theme or release
Code that crashes the PHP process itself, rather than just throwing an error, shows up as 502 instead of 500.
A firewall or rate limit
A firewall or security module that blocks the proxy’s IP addresses cuts the link, even when the site itself is healthy.
Try it yourself
Safe first steps for a 502 Bad Gateway
These checks are safe. Do them in order and stop when the site is back.
- 1 Wait a minute, reload, and test another page and another network. A 502 that comes and goes under load is a different problem from one that stays.
- 2 If you use Cloudflare or another CDN, pause it briefly (or set DNS-only) and reload. If the site now loads, your server is fine and the issue sits in the proxy settings or a firewall. If it still fails, the origin is the problem. Switch the CDN back on afterwards.
- 3 Check whether PHP is running. In your hosting panel, restart PHP or the app; on your own server run systemctl status php-fpm (the service name usually includes the version, such as php8.3-fpm).
- 4 Read the logs: the nginx error.log and the PHP-FPM log. “Connection refused” means the upstream is down, “upstream timed out” means it was too slow, and “server reached pm.max_children” means it is overloaded.
- 5 On WordPress, rename wp-content/plugins to plugins-off to rule out a plugin. If the 502 started after a PHP upgrade, step back to the previous PHP version in your hosting panel.
- 6 If you moved servers or changed nameservers recently, check that the origin IP address in Cloudflare matches the one your host gave you.
Our approach
How we fix a 502 Bad Gateway
-
1
We find the failing link
We follow the request from gateway to application, using the proxy and PHP logs, so we know which link broke, not just that one did.
-
2
Backup before any change
A full copy of files and database comes first, so every change can be undone.
-
3
Cause, fixed price, your OK
The diagnosis is free. You get the cause in plain language, a fixed price and a time estimate. Nothing starts without your yes.
-
4
Fix, load-test and report
We repair the cause, check that the site holds up under normal traffic, and send a short report of what was wrong and what we changed.
What we need
What we need from you
- Access to your hosting panel or SSH, including the nginx/Apache and PHP logs
- Cloudflare or CDN access, if you use one
- An admin login for your CMS or application, if there is one
- When the 502 started, whether it is constant or intermittent, and what changed before
We ask for the least access that does the job, use a separate key per client, and you can revoke it the moment we are done.
What we cannot fix
We cannot fix a 502 that originates in your hosting provider’s own network or a managed platform where we get no server access. If that is the cause, we tell you after the free diagnosis, with the evidence to take to your provider.
Price
Price and what happens next
-
1
Free diagnosis
We find the cause and tell you what is wrong.
-
2
Fixed price
You get a price and a time estimate. Nothing starts without your yes.
-
3
Fixed and reported
We repair it, test it and send a short report.
From €295 excl. VAT
Fixed price, agreed before we start
Is it urgent? Say so in the form or in the chat. We start as soon as we can and give you a realistic time estimate with the quote. If priority work costs extra, you see that in the quote before you agree.
Get it fixedQuestions?
502 Bad Gateway: common questions
Also see
Related problems
Website down
A website is down when the server cannot deliver your pages to visitors. The most common causes, in order of likelihood, are an expired doma...
Read more →Hacked website
A hacked website is a site where someone else has planted code, files or admin accounts without your permission. Typical signs are redirects...
Read more →Last updated:
Request
Tell us what is wrong
Answer a few short questions. You get a fixed price before we do anything.
Thank you for your request!
We will contact you shortly
An error occurred!
Please try again.