How to Fix the “502 Bad Gateway” Error in WordPress
Troubleshooting errors is one of the harder tasks every WordPress website owner has to face. Despite WordPress being quite an easy platform to work with, errors do happen. The more common ones are often accompanied by a catchy name or an HTTP status code.
In this article, we will discuss the 502 Bad Gateway error. Even though it isn’t as common as the 500 Internal Server error or 503 Service Temporarily Available error, it can be as difficult to solve as its cause can vary. In this article, we will take a close look at what the 502 Bad Gateway error is, the reasons why it happens, and all the surefire ways for solving it. If you’d like to skip to any specific part of the article, just click on one of the links below:
When you try to access a website, your browser sends requests to the server hosting the website. The server should send the website content and an appropriate response code in return. According to Mozilla’s definition of the error, the 502 Bad Gateway server error response code indicates that the server, while acting as a gateway or a proxy, received an invalid response from the upstream server.
However, this is a slight simplification of the problem. Most hosting setups nowadays include multiple servers: application servers, database servers, reverse proxy servers, web servers, and CDNs. Therefore, an error that implies an invalid response from one of the servers can happen for a multitude of reasons. Those include database issues, server timeout issues, faulty plugin or theme code, issues with the proxy server, web server overload, as well as browser cache issues, and many more.
The error messages you could encounter are as diverse as the possible reasons behind the 502 error. They include 502 Bad Gateway nginx, 502 Proxy Error, Error 502, HTTP 502, 502 Service Temporarily Overloaded, 502. That’s an error, 502 bad gateway Cloudflare, and so on.
Since there are a multitude of possible reasons behind the error, it can prove challenging to solve quickly. However, speed is essential. Having this error on your website for an extended period or occurring frequently can negatively affect your SEO ratings.
How to fix the 502 Bad Gateway error
The appearance of the 502 Bad Gateway error usually indicates some kind of a server-side issue. However, as it can sometimes be caused by a client-side issue, it is important to run tests to determine the real cause. In this section, we will show multiple ways to troubleshoot the error, going from the methods that are easier to implement and moving on to more complex ones.
The easiest troubleshooting step and the one you should do first when facing a 502 Bad Gateway error is to simply wait a couple of minutes and then try to reload the page. You can do that by pressing the Reload button in your browser or using the keyboard keys—F5 for Windows and Command + R for Mac. If this solves the error, then you don’t need to review the rest of the article.
However, if you experience the 502 error frequently, we suggest reading on. Our troubleshooting steps can help you resolve any underlying issues leading to 502 errors. Before we proceed, we also suggest testing your website on Down for Everyone or Just Me.
To do so, you simply need to insert the website URL in the appropriate option and press the Or just me? button, as shown in the image below.
This will help you establish if the issue is only on your side or not.
If reloading the page didn’t solve the issue, the next step is to clear your browser’s cache. This helps you avoid cases where the error screen is still showing because the page is being loaded from the cache, but the error itself is resolved.
If you are experiencing a 502 error only on a single page, we suggest performing a hard reload/refresh of that page. It reloads the page while deleting the browser cache for that specific page. Windows and Linux users can do this by pressing the CTRL and F5 keys at the same time. And MacOS users can do it by pressing the CMD, Shift, and R keys simultaneously. We recommend performing a hard reload before you test your site and after trying any of the suggested troubleshooting methods.
If this error isn’t limited to a single page only, we suggest fully clearing your browser cache. You might already be familiar with the process, but we’ll show you the steps with the Chrome browse, just in case. Keep in mind, different browsers can display the same options in different ways.
To clear your browser cache in Chrome, click on the three dots located in the upper-right section of your screen. Then, click on the History option in the menu, and click again on the newly opened History option in the submenu. For users who prefer keyboard shortcuts, the same can be achieved by pressing the CTRL and H keys simultaneously.
Then, press the Clear browsing data option on the left which will open a new page, as well as a popup window within it.
Within the popup window, choose what you would like to delete, and set the time range for it. By default, you will be placed in the Basic tab. However, you can switch to the Advanced tab if you want more control over the things you wish to delete. After choosing the appropriate options, press the Clear data button to clear the cache.
Wait until the task is completed. Then, reload your website and check if the error has disappeared. You can also check whether the issue was caused by the cache if you open the website in a different browser.
DNS or Domain Name System is how domain names are linked to appropriate IP addresses. If a domain isn’t matched with the appropriate IP address, it leads to a 502 Bad Gateway error. This is important to keep in mind when migrating your WordPress website, as the DNS name server will need to keep up with the shift. But, processing these changes depends on the TTL setting in your DNS records, and it may take between 24 and 36 hours to propagate.
You will need to wait for these issues to resolve automatically. After that, you should test your website to verify that the error has disappeared.
If you keep seeing the 502 error, you should consider flushing your local DNS cache. This serves a similar purpose as clearing your browser cache. Windows users can flush their local DNS cache from the Command Prompt, by running the ipconfig /flushdns command. And macOS users can flush it using the terminal window to run the dscacheutil -flushcache command.
Most website owners use CDNs or content delivery networks to serve static website resources (images, CSS, and JS stylesheets). This makes CDNs one of the most valuable assets for improving your website speed. However, an issue on their servers or a misconfiguration can cause the 502 Bad Gateway error.
To find out if this is the underlying issue behind the 502 error, you would need to temporarily disable the CDN service. As most WordPress users employ a plugin for integrating the CDN service, you would need to disable that plugin via FTP to disable your CDN.
If the 502 error disappears afterward, it confirms that the error was caused by your CDN service. To solve it, you need to contact your CDN provider and ask for assistance. After the issue is resolved, you can simply re-enable your CDN service.
Additionally, sometimes hosting providers will offer protection against DDoS (Distributed Denial-of-Service) attacks in the form of a firewall. This firewall can block requests coming from the CDN server and cause the 502 Bad Gateway error. In that case, to solve the error, you need to disable the firewall or adjust its configuration not to block requests from your CDN.
PHP timeout issues happen when a single PHP process runs for more than the max_execution_time setting allows it to. It usually happens when you try to import larger files in WordPress. When the issue happens, it is followed by the 502 Bad Gateway error. Luckily, these types of 502 errors can be solved quite easily by increasing the max_execution_time server resource.
In most cases, setting the value to 300 seconds or higher will solve the issue. However, to do so, we suggest contacting your hosting provider regarding the increase. You might need to negotiate the max_execution time increase, as it can be limited by your current hosting plan.
Poor code is often the reason why scripts and queries get terminated by your server. As such, it is important to test your themes and plugins thoroughly. For example, caching plugins are a likely suspect when investigating the 502 error.
To check if this is the case for you, we will show below how you can deactivate your themes and plugins via FTP. However, the process of deactivating a theme has an important prerequisite – you need to have a default theme installed for testing purposes. When you deactivate your current theme, WordPress will need a theme to fall back on. Therefore, if you haven’t done so already, make sure to install a theme from WordPress, such as TwentyTwenty. After that, you can follow the steps below.
To deactivate your current theme, connect to your server using your FTP credentials and navigate to your root WordPress directory, often called public_html.
Then, navigate to the /wp-content/themes directory and locate your current theme’s folder. Right-click the folder and press the Rename option.
Rename the folder to deactivate the theme. We suggest keeping the original name of the theme, to avoid further confusion. So you can add a self-explanatory suffix like .deactivated, .backup, or .old while renaming your folder.
Renaming the theme folder will deactivate your current theme. This will make your previously-uploaded default theme the active theme on your website.
Afterward, check the website by reloading it to see if the 502 error has disappeared. If it has, then the fault lies in the theme you were using. You should contact its authors and ask for help in resolving the 502 Bad Gateway error. If the error remained after you deactivated the theme, it means your theme is not to blame and you should one of our other troubleshooting suggestions.
In the same vein, you can test your plugins by deactivating them via FTP. To do so, find your root WordPress directory and open the wp-content directory. Then locate the plugins folder within, right-click on it, and press the Rename option.
Again, we suggest keeping the original name and only adding a suffix such as .deactivated or .old.
Renaming the plugins folder will deactivate all your plugins. More precisely, it will deactivate them and make them inaccessible from your admin dashboard.
You should, then, reload your website and inspect whether the 502 Bad Gateway error is gone. If the issue is still present, then its cause doesn’t lie in your plugins and you should try some of the other suggested solutions.
But, if the error is gone, it means the culprit was one of your installed plugins. In that case, the only thing that remains is to pinpoint which one it is.
To find the exact plugin causing the error, restore the plugins folder’s original name—plugins. This won’t activate your plugins, but it will make them accessible from the dashboard.
Then, log in to your admin dashboard using your WordPress credentials and navigate to Plugins > Installed Plugins. You should activate your plugins one at a time. After activating each, you should reload the page where you were checking for the 502 error. Repeat the process until you find the faulty plugin. You’ll know which one it is if the 502 Bad Gateway error re-appears after you activate it.
When you’ve found the plugin that is causing the 502 Bad Gateway error, you should contact the plugin authors about solving the error.
Sometimes, even after going through all the previously suggested solutions, the testing might be inconclusive and the true origin of the 502 error a mystery. On those occasions, examining the error log can give you valuable insight into all the issues that happened on your website. Depending on which file paths are mentioned in the error log, you have a few options. You can contact your theme authors, the appropriate plugin authors, or your hosting provider for further help.
Contacting your hosting provider is often an integral part of solving any WordPress error. Even if you weren’t able to solve the 502 Bad Gateway error with the previous troubleshooting suggestions, you should still contact your hosting provider.
Explain your issue thoroughly, as well as all the steps you’ve taken. After exhausting all the common reasons for the error, the next most likely cause is a web server-side issue. In that case, they can let you in on what happened and help you efficiently and quickly solve the error.
After reading this article, you should have a clearer understanding of what a 502 Bad Gateway error in WordPress is and the many reasons it can occur. However, if you follow our troubleshooting suggestions, we are confident you will solve it in no time. If you are concerned about the issue reoccurring, feel free to bookmark this article for future use.