Managing websites built with WordPress is easy, but it is equally frustrating to troubleshoot the errors, which can occur at any time. Just like prescribing the right drug to fight effectively a disease require complete knowledge about that disease; it is also essential to know the error and its causes before troubleshooting it.
Today, the error in the discussion is “ WordPress 502 Bad Gateway Error”, which is capable enough to break your website and takes some valuable hours of troubleshooting from your busy day.
Table of Contents
- What Is 502 Bad Gateway Error?
- Why 502 Bad Gateway Error Occurs in WordPress?
- Fix 502 Bad Gateway Error in WordPress
- Check If Web Host Is Responsive
- Reload the Page
- Clear Browsing Cache
- Look for DNS Issues
- Fix the Local DNS
- Disable CDN
- Audit Themes and Plugins
- Check Logs
- PHP Timeout
- Check Scripts
What Is 502 Bad Gateway Error?
To understand 502 Bad Gateway Error, you should be aware of how a browser loads data from the server. When a user visits a website, the server sends a request to fetch the data required to display the content in the browser.
If the server responses accurately to the requests, it then receives requested content, otherwise, 502 Bad Gateway Error screen comes up.
WordPress websites run on multiple servers allocated to a specific task. For example:
- Database server for WordPress databases
- Application server to run PHP and other app-related content
- The reversed proxy server responsible for Nginx, Apache, Varnish
- Web server to serve website files
The multiple servers together can complicate the way the browser handles the requests. Upon receiving the request, reversed proxy server go and fetch data from origin servers like a database server, application server, and web server.
If any one of these servers sends an invalid response, then reversed proxy server comes back to the browser and display 502 Bad Gateway Error.
We often see 502 Bad Gateway Error differently on different websites, but they all point the same thing: the server was unable to respond to the requests. Let us look at some of the examples this error shows.
- “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”
- “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.”
Twitter displays 505 Bad Gateway Error in a friendlier way.
Google displays 505 Bad Gateway Error with a suggestion of trying again after some time.
Now that we have understood what is 502 Bad Gateway Error and how different websites display it. Let us move ahead and explore what causes this error and how can we fix it in WordPress?
Why 502 Bad Gateway Error Occurs in WordPress?
We get 502 Bad Gateway Error when the server fails to respond accurately and returns the request with invalid information. However, there are other possibilities as well and to know them is important if you wish to troubleshoot this error effectively.
- Your server is busy with other tasks or hit by major traffic from a particular website (in case of shared hosting)
- Origin servers are unresponsive or inaccessible
- Errors in the database
- Problem with the reverse proxy server
- Corrupt PHP scripts in files such as WordPress themes and plugins.
- CDN is acting up
- DNS issues
All of the above possibilities can result in WordPress 502 Bad Gateway Error. However, troubleshooting any error gets trickier if you do not know the elements involved in triggering that error.
Although we have established a complete understanding of what is 502 Bad Gateway Error and why does it happen? It gives us a ticket to our next section, which covers the fixes for this error.
Fix 502 Bad Gateway Error
Check If Web Host Is Responsive
The first thing you need is to check if your web server is responsive or not. It is not always something terrible happening to your WordPress website.
The easiest way of finding out if your server is indeed the culprit is to run an online scan. Visit site24x7 and enter your web server name.
The multiple locations will ping your server and show you the stats like Load time, Package Loss and Response time.
Another way of checking the responsiveness of your web server is to ping it manually using SSH.
Simply access the SSH, terminal and run the following command:
ping (server IP)
Now, replace “server IP” with your server IP address.
If your server is responsive, it will show the ping on each interval.
Sometimes any query or task takes more time than usual; server cancels it and returns with 502 Bad Gateway Error. It is common in shared hosting and the provider kills the script to avoid any negative impact on other websites hosted on the same server.
If you are hosting your WordPress website on Cloudways, then chances of getting this error are low. That is because you are the only owner of your server and control the websites hosted on it and we use a powerful stack that can cope with a load on servers.
However, if you still encounter such issues, Cloudways 24/7 live chat support can help you recover from this situation.
Reload the Page
Sometimes just by reloading the web page, you can save yourself hours’ worth of troubleshooting. That is right!
It is possible that you get an overloaded server for some time and when you check back in a minute or so, it gets back on.
Also, confirm if the website is down just for your or for everyone else as well. To do this, simply test the URL on Down For Everyone Or Just Me.
If the website is up for everyone else, then the problem is at your end. Try switching the web browser.
Clear Browsing Cache
The browser is capable of caching or storing website data so the next time user visits, it does not have to re-download the same static resources all over again. It is an effective way of utilizing resources, but it also has some drawbacks.
Let us say you went to a different browser and the web page loaded just fine. Knowing that your web page loads correctly, you come back to your default browser, but the issue persists.
That is because your browser is displaying you the cached copy of that web page. To fix that, simply clear your browser cache.
Look for DNS Issues
Domain Name System or short for DNS is a way of linking domain name with an IP address. If the domain fails to resolve to correct IP, it can trigger 502 Bad Gateway Error. We can see this error when migrating WordPress website to a new host, which requires changes in DNS name server.
The changes can take between 24 to 36 hours to propagate. During this period, if you try to access your site, you might get WordPress 502 Bad Gateway Error.
Fix the Local DNS
We can clear DNS cache from Windows Command Prompt. In Windows OS, run ipconfig/flushdns command to clear the DNS cache. In Mac OS, run the command as dscacheutil -flushcache inside the terminal window.
Content Delivery Network (CDN) is widely used to serve static content of the website. It is a useful technique to deliver web content faster to a global audience. However, CDN can be one the reasons of WordPress 502 Bad Gateway Error due to an extra layer between the server and browser.
To cross this from your list of possible causes, simply disable the CDN service and then try to access your website. If the website loads properly, then you have caught the culprit. You just need to contact CDN support and if not, then we suggest you to keep following the rest of the article.
Audit Themes and Plugins
Themes and plugins play an essential part in any WordPress website. It is nearly impossible to have a fully functional website without adding themes and plugins. They can also become a reason of WordPress 502 Bad Gateway Error.
Themes and plugins, which contain incorrect PHP code, can cause issues that are either not understood by the server or takes too much time to process. This result in 502 Bad Gateway Error.
Start by deactivating all the plugins from FTP. Simply access your root directory where your WordPress files are installed, i.e., public_html folder, navigate to wp-content → plugins and rename the plugins folder, so it is inaccessible by WordPress system.
Now check your website and if loads properly, then start activating plugin one by one until you find the one causing problems.
If you don’t find any faulty plugin, then go ahead and look for a faulty theme. Inside your wp-content folder, navigate to themes and rename your activated theme to deactivate it. Removing it might not be the suitable option as uploading it again can lose custom styling.
Now go back to your website and if you see a white screen instead of an error, then Vola! Your theme was causing the issue. Log in to your wp-admin and activate the default theme.
WordPress logs are useful when it comes to troubleshooting the errors. We can enable the error logs by adding the following code inside wp-config.php file.
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );
The logs are located inside wp-content folder. Cloudways users can take advantage of the integrated monitoring tool New Relic for precise and comprehensive analysis.
When PHP reaches the allowed maximum time in server configuration to execute a script, it shows an error. It can sometimes trigger WordPress 502 Bad Gateway Error. To avoid this, simply increase the maximum execution time value. By default, it is set to 300 seconds, which is 5 minutes.
We create custom functionality and themes in WordPress. These custom functions can be the reason why your server is unable to process them. Also, calling third-party APIs such as Google reCAPTCHA, which generates long tokens can put stress on the server, and it returns with 502 Bad Gateway Error.
We hope after reading this article, 502 Bad Gateway Error will not look so scary the next time you see it. The troubleshooting is easy if you can predict the source and use the techniques listed in this article to fix it. We can still fix WordPress 502 Bad Gateway Error just by reloading and clearing the browser cache, but not every day is a sunny, so it is wise to prepare for the dark.
Ibad Ur Rehman is a WordPress Community Manager at Cloudways. He likes to explore latest open-source technologies and to interact with different communities. In his free time he likes to read, watch a series or fly his favorite Cessna 172SP in X Plane 10 flight simulator.
Be the first to check out WordPress 5.0
Use our Staging feature before upgrading your website to WordPress 5.0.