What Is a 502 Bad Gateway Error (and How to Fix It)
A 502 Bad Gateway error is another 5xx server error, but it's more specific. It means your request was successfully received by a "gateway" server (like a CDN, proxy, or load balancer), but that server received an invalid or "bad" response from the main "origin" server (your web host).
What Does "Bad Gateway" Mean?
Imagine you call a company's front desk (the gateway). You ask to be transferred to the sales department (the origin server). The front desk tries to connect you, but the sales department's line is broken or sends back a garbled message. The front desk (gateway) then hangs up and tells you, "I'm sorry, I got a bad response from that extension."
This is a 502 error. It's almost always a server-to-server communication problem. This is very common if you use a service like Cloudflare.
How to Fix 502 Errors
As a visitor, you can try refreshing the page after a minute or clearing your browser cache, but the problem is almost certainly not on your end.
As a site owner, this error means your origin server is the problem. The gateway (e.g., Cloudflare) is healthy, but your web host is not.
1. Check if Your Origin Server (Host) is Down
Your main web hosting server might be offline, rebooting, or experiencing an issue (like a 500 or 503 error). The gateway tries to get your site, gets the error, and shows a 502 to the user.
- How to fix: First, check your server's status. If you use Cloudflare, you can "pause" it to see the real error. If you see a 500 or 503 error after pausing, you must fix that underlying issue first. If the server is just down, contact your host.
2. Check Server Overloads
Just like a 503 error, your origin server might be overloaded with traffic or a runaway script. The gateway's request times out while waiting for a response, so it gives up and sends a 502 error.
- How to fix: Check your server's resource usage (CPU, RAM) in your hosting panel. Look for traffic spikes. You may need to upgrade your hosting plan or find a plugin that is causing the overload.
3. Check Your Firewall
Your server's firewall (or a security plugin) might be mistakenly blocking requests from your gateway service. It sees the requests from Cloudflare's IPs as a threat and blocks them, leading to a 502 error.
- How to fix: You must "whitelist" the IP ranges of your CDN or gateway service. Cloudflare, for example, publishes a list of their IP ranges. Add these to your firewall's "allow list" so it never blocks them.
4. Check Server Error Logs
As with a 500 error, your server error log is your best friend. A PHP script that is crashing or timing out will be logged here. This can point you to a specific plugin or theme function that is failing and causing the origin server to send a "bad" response. Check your error_log file in your hosting panel.