502 Bad Gateway Error
A 502 Bad Gateway error means a server acting as a proxy or gateway, such as Nginx, a CDN, or a load balancer, successfully reached the backend server behind it but got back an invalid or malformed response. The problem usually sits with the backend server, for example a crashed PHP-FPM process or an application that returned something the proxy couldn't understand, rather than with the proxy itself or the visitor's device.
What is actually happening
502 belongs to the 5xx family of status codes, meaning the problem is server-side. Many hosting setups put a proxy in front of the actual application, whether that's Nginx forwarding requests to PHP-FPM, a CDN like Cloudflare proxying to your origin server, or a load balancer distributing traffic across several backend servers. A 502 specifically means that front-facing layer reached the backend, but what came back wasn't a response it could pass along, often because the backend crashed mid-request, closed the connection unexpectedly, or sent back something malformed.
Common causes
| Cause | Where it typically shows up |
|---|---|
| PHP-FPM process crashed or not running | Nginx reverse-proxying PHP requests |
| Backend server overloaded or out of memory | Any proxy or load balancer setup |
| Backend took too long and the connection was dropped | Slow database queries or external API calls |
| Misconfigured proxy_pass or upstream address | Nginx or Apache acting as a reverse proxy |
| Firewall blocking the port between proxy and backend | Multi-server or load-balanced setups |
| Origin server down or unreachable | CDN or WAF in front of the real server, like Cloudflare |
How to fix it
- Check whether the backend service is running. For a PHP-FPM setup on Linux:
sudo systemctl status php-fpm
sudo systemctl restart php-fpm
- Check the proxy's error log, which usually names the exact upstream failure:
sudo tail -n 50 /var/log/nginx/error.log
- Look for resource exhaustion on the backend, such as the server running out of RAM or hitting a process limit, which is a common cause on busy shared or under-sized VPS plans.
- Verify the proxy configuration points to the correct backend address and port, especially after any recent server or network change.
- If a CDN or WAF is involved, confirm the origin server is actually reachable and responding directly, bypassing the CDN temporarily to test if needed.
502 compared to other gateway errors
- 500 Internal Server Error — the application itself hit an error while running, with no proxy involved in the failure.
- 502 Bad Gateway — a proxy reached the backend but got an invalid response back.
- 503 Service Unavailable — the server is temporarily unable to handle requests at all, often due to overload or maintenance.
- 504 Gateway Timeout — a proxy gave up waiting because the backend never responded in time.
What visitors can do
502 errors are almost never caused by the visitor's own device or connection. Refreshing the page or trying again after a minute sometimes works, since it may be a brief backend hiccup, but if the error persists, there's nothing on the visitor's end that will fix it; it needs attention from whoever manages the site's server.
Frequently asked questions
What causes a 502 Bad Gateway error?
It happens when a proxy, load balancer or CDN successfully contacts the backend server but receives an invalid or malformed response, often because the backend crashed, timed out, or ran out of resources.
How do I fix a 502 Bad Gateway error on my own site?
Check that backend services like PHP-FPM are running, review the proxy's error log for the specific failure, and look for resource exhaustion on the server, then restart the affected service.
Is a 502 error caused by my computer or the website?
It's almost always a server-side issue with the website's hosting setup, not a problem with your device, browser or internet connection.
What is the difference between a 502 and a 504 error?
A 502 means the proxy got a response from the backend but it was invalid, while a 504 means the proxy gave up waiting because the backend never responded within the time limit at all.
Why do I see a 502 error through Cloudflare but the site works when I visit the server directly?
This usually points to a connectivity or configuration issue specifically between Cloudflare and your origin server, such as a firewall blocking Cloudflare's IP ranges or an SSL mismatch, rather than a problem with the site's code.
Does refreshing the page fix a 502 error?
Sometimes, if the cause was a brief, one-off backend hiccup. If the error keeps happening on repeated refreshes, the underlying server-side issue needs to be fixed rather than worked around.
Related topics
- What Is Nginx? Web Servers
- What Is a CDN (Content Delivery Network)? Performance & CDN
- What Is a Load Balancer? Web Servers
- What Is PHP-FPM? Web Servers
- What Is a Database? Databases