If you are seeing the 502 Bad Gateway error on your WordPress site, you’re probably confused and wondering what’s plaguing your beloved site.
Not to worry though, the 502 Bad Gateway error affects a large portion of the internet including big services such as Google, Twitter and so on.
Most times, this error usually resolves itself in a few minutes, but if it doesn’t go away, we show you exactly how to troubleshoot and fix it.
That out of the way, grab yourself a hot mug of coffee and let’s get this over and done with.
Have a question or suggestion regarding the 502 Bad Gateway error? If that’s a resounding yes, please don’t hesitate sharing with us in the comments.
What Does 502 Bad Gateway Mean?
Modern day web applications including your WordPress site are hosted on multiple servers. The most popular server setup supporting a WordPress site involves a couple of servers namely:
- A database server that runs your WordPress databases
- The application server that runs the login, PHP code and any other dynamic content your WordPress site relies on
- A reversed proxy server (also known as a gateway) running software such as Nginx (read as Engine-X), Apache and Varnish among others
- Web server that serves your website files
Now when you open your WordPress site in your browser, it triggers multiple requests for all the parts that come together to generate the page you see in your browser window.
These requests are sent to the reverse proxy server, which determines where to send the requests. The reverse proxy server then retrieves:
- PHP and other dynamic content from the application server
- Your MySQL databases from the database server
Here is a simple chart we prepared for illustration purposes:
Note, servers sitting behind the reverse proxy server are known as upstream or origin servers.
But where is our 502 error in all this? A 502 Bad Gateway error happens when the reverse proxy server receives an invalid response from any of the upstream servers.
The error message you see in your browser window comes from the reverse proxy server, which is simply server-speak for “I tried but failed to retrieve the content you need.”
So, in other words, the upstream servers place blame on the reverse proxy server because, well, you aren’t supposed to know your WordPress site is served from different servers. All you know is you have a server, and that’s enough.
Different Messages, Same Meaning
Speaking of the bad gateway error message, it comes in many flavors such as:
- “502 Bad Gateway”
- “HTTP Error 502 Bad Gateway”
- “502 Service Temporarily Overloaded”
- “Error 502”
- “502 Proxy Error”
- “HTTP 502”
- “502 Bad Gateway NGINX”
- “502 Server Error: The server encountered a temporary error and could not complete your request” – This one sorts of tells you the server is acting up, but it doesn’t specify which server
- “502. That’s an error. The server encountered a temporary error and could not complete your request. Please try again in 30 seconds. That’s all we know.”
Other web services such as Twitter…
…and CloudFlare might customize the error message:
If you see any of the above messages, just know you’re deep in the throes of a 502 Bad Gateway error.
Not to worry though, we have your back. Now, let’s see what causes the 502 Bad Gateway error lest we shoot ourselves in the foot with foolish solutions.
Causes of the 502 Bad Gateway Error
Most times, this error happens when there’s poor communication between your servers, something you clearly have no control over.
However, this error can happen due to a number of reasons such as:
- One/all of your upstream servers are down or can’t be accessed
- Your server is simply overloaded
- You have buggy PHP script such as faulty WordPress plugins or themes
- Browser cache is acting up
- Problems with your router or networking equipment
- Your content delivery network (CDN) is acting up
- You have domain name system (DNS) issues
Now let us fix that 502 bad gateway error that’s causing you so much anguish.
Fixing 502 Bad Gateway Error
We shall troubleshoot each of the above problems and offer the most practical solution in each case. If it’s any consolation, there’s no way in this world the 502 Bad Gateway error on your WordPress site is as a result of all of the above issues.
All the same, you can’t tell which problem is causing the error, meaning you have to go through each solution until you get something that works for you. We shall begin with the easiest solution.
Reload Your WordPress Site
Before you freak out, perhaps the 502 Bad Gateway error you’re encountering isn’t as serious as you imagine. Perhaps your host server was simply overloaded, in which your site should be back up and running in a minute or so.
Before you attempt anything else, wait for a few minutes and reload your website. As you wait, you can paste your URL into the isup.me tool to determine if your site is down for everyone or just you.
If the website is up for everyone but down for you, the problem is on your end, in which case you should begin the diagnosis by trying a different browser.
Clear Browser Cache
Let’s say you tried a different browser and to your amazement, the 502 Bad Gateway error is not there. When you go back to your favorite browser, however, the error reappears all of a sudden. What could be wrong?
If this is the case, you simply need to clear your browser cache. Clear your browser what? Well, modern browsers save files from the websites you frequent on your computer.
Why? Next time you visit a cached website, the browser doesn’t need to request the same files from the server all over again unless, of course, you’ve updated said files. Your browser caches files that don’t change often such as images, CSS files and so on.
An aggressive caching solution might save the 502 Bad Gateway error to your browser, which might erroneously inform you have a problem while in fact you just have a misbehaving caching solution that hasn’t bothered to refresh changes. Cleaning your browser cache is simple as we detail in this How to Clear WordPress Cache post.
Restart Your Networking Equipment
If changing browsers and cleaning browser cache prove unfruitful, it is time for one of the most common troubleshooting steps known to humanity; restarting whatever isn’t working properly.
Issues with your networking equipment can leave you with a 502 Bad Gateway error and a bad taste in your mouth. By the way, did you know most home networks have a gateway?
Oh yeah they do, especially if your setup involves a router. Your router’s gateway could be the source of your bad gateway error.
Simply unplug (remove the power cable) your networking equipment, wait 45 seconds and plug the equipment back in. If you have a router + modem hybrid, plug in the modem first and wait at least 60 seconds before plugging in the router.
After you turn on the router, wait at least 2 minutes. Then test whether the error went away. As you wait, restart your computer or whatever device is showing the error.
Disable Your CDN Memontarily
CDN (short for content delivery network) is a service that serves files on behalf of your WordPress site hence boosting site performance.
A good example of a top-notch CDN is CloudFlare, but their free plan causes the 502 Bad Gateway once a while, since they have extra firewalls in between.
A 502 Bad Gateway error caused by CloudFlare looks like this:
Troubleshooting whether your CDN is the cause of your current predicament is easy since you just need to disable the service temporarily and test if the error is gone.
If the problem is your CDN, you should contact them for support. Moving on swiftly.
What’s with all these big words hombre? What does DNS even mean? DNS (short for domain name system) is, according to Verisign, “…a central part of the internet, providing a way to match domain names (e.g. 000webhost.com) to IP addresses (e.g. 18.104.22.168).
If your domain does not resolve to the correct IP address, you could end up with a 502 Bad Gateway error. Have you migrated your WordPress site to a new host recently? Migrating to a new host usually involves editing your DNS name server.
The changes take anywhere between 12 and 36 hours to propagate, which could result in the bad gateway error. Best is to let domain propagation to complete succcessfully.
Clear Local DNS Cache
While at it, clear your local DNS cache as well. How? If you’re using Windows OS, simply open the Command Prompt (cmd.exe), enter
ipconfig /flushdnsand press the ENTER key:
If you’re a Mac user, run the command
dscacheutil -flushcachein the terminal.
Change Your DNS on Your Router & Devices
Seeing as we are talking about DNS, consider changing DNS servers on your computer, device or router since temporary DNS issues in these devices can cause gateway errors. You can use free and public DNS servers such as Google and Verisign among others.
Fix Your WordPress Plugins & Themes
Bad PHP code by faulty plugins and themes can trigger scripts and queries that “confuse” your server. When this happens, the server cancels these processes since they can take out your site or the server itself. This results in bad gateway errors.
Other times, the error is not as a result of bad code. We have seen plugins using third-party services such as Google reCAPTCHA causing the 502 Bad Gateway errors. Sometimes, NGINX rejects the extremely long tokens Google generates when showing the reCAPTCHA challenge.
All in all, let’s diagnose whether your plugins are the cause of your problem. If you cannot access the WordPress admin, you need to access your WordPress root directory via an FTP app such as FileZilla:
Next, navigate to wp-content -> plugins, and rename the plugins folder to something like plugins_old:
This will deactivate all of your plugins at once. Note that you won’t lose any data. Reload your WordPress site to see if deactivating your plugins via FTP fixes the error. If the error disappears, rename plugins_old back to plugins, and log in to your WordPress admin.
Navigate to Plugins and reactivate each plugin while reloading your site after each activation to find the culprit. Remove the problematic plugin, find an alternative or/and contact the developer for support.
If your plugins aren’t the problem, perhaps your theme contains the buggy PHP code that’s wreaking havoc on your site. Login to your WordPress root directory via FTP:
Navigate to wp-content -> themes -> Your_Active_Theme. Right-click on your active theme and download a copy to your computer as backup:
Next, right-click on your active theme and delete it from your WordPress root directory:
Now, refresh your site. If you see a white screen instead of the error, your theme is the problem. Login to your WordPress admin and activate a default theme while you fix your problematic theme.
Update Your WordPress Core, Theme & Plugins
Outdated software exposes you not only to this kind of errors, but also security risks of all types.
That said, always update your WordPress core, themes and plugins to the latest versions. This way, you can get rid of potentially bad code that could jam your server.
Check if Your Server is Online
Are you still seeing the 502 error after trying the above solutions? If so, the problem might be an unresponsive server. While there isn’t much you can do if any of your upstream servers is unreachable for one reason or another, knowing the error stems from your server could offer you some much-needed peace of mind.
You can use a ping test or trace-route to detect if your server is acting up. If it is, make yourself a mug of coffee and wait. If your server is okay, perhaps you missed a step in this tutorial or something else is causing the error, in which case you can let us know via the comment section and we will offer you the help you need.
Fixing the 502 Bad Gateway error is as simple as A, B, C considering it usually resolves itself in no time. However, it might prove a bit challenging for a beginner since this error can be a result of a number of reasons.
Regardless, we hope this guide helped you to fix the 502 Bad Gateway error on your WordPress site. If you have any question or suggestion, we look forward to reading your thoughts in the comment section. Cheers to a working website!