How to Fix 502 Bad Gateway Error in WordPress

Managing WordPress-based websites is simple, but troubleshooting issues, which can arise at any time, can be tedious. Just like giving the correct treatment to effectively fight a sickness necessitates a thorough understanding of that disease, it is equally necessary to understand the error and its causes before troubleshooting it.

The error in question today is the “WordPress 502 Bad Gateway Error,” which has the potential to ruin your website and waste hours of your time troubleshooting.

How to Fix 502 Bad Gateway Error in WordPress

To comprehend the 502 Bad Gateway Error, you must first comprehend how a browser retrieves data from a server. When a user visits a website, the server makes a request to the user’s browser to retrieve the data needed to show the content.

The server receives the requested content if it responds appropriately to the requests; else, the 502 Bad Gateway Error screen appears.

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 use of numerous servers can make the browser’s handling of requests more difficult. When a reversed proxy server receives a request, it attempts to retrieve data from origin servers such as a database server, application server, or web server.

If any of these servers delivers an invalid answer, the browser is redirected to the inverted proxy server, which displays a 502 Bad Gateway Error.

On different websites, we frequently see 502 Bad Gateway Errors, but they all mean the same thing: the server was unable to reply to the requests. Let’s have a look at some of the scenarios in which this problem occurs.

  • “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 know what a 502 Bad Gateway Error is and how it appears on different websites. Let’s have a look at what’s causing this error and how we may repair it in WordPress.

Why Does WordPress Throw a 502 Bad Gateway Error?

When a server fails to react appropriately and returns inaccurate information, we get a 502 Bad Gateway Error. There are, however, additional alternatives, and being aware of them is critical if you want to properly debug this mistake.

  • 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

WordPress 502 Bad Gateway Error can be caused by any of the aforementioned scenarios. However, debugging any mistake becomes more difficult if you are unaware of the elements that contributed to the error.

Despite the fact that we have a thorough understanding of what a 502 Bad Gateway Error is and why it occurs, It directs us to the next part,Error 502: Bad Gateway

Examine If Your Web Host Is Responsive

The first step is to determine whether or not your web server is responsive. It is not always the case that something bad happens to your WordPress site.

Running an online scan is the quickest way to see if your server is the problem. Enter your web server name at site24x7.com. which discusses how to correct this issue.

Pinging your server from several locations will display statistics such as load time, package loss, and response time.

Another technique to test your web server’s responsiveness is to manually ping it via SSH.

Simply open a terminal with SSH and type the following command:

ping (server IP)

Now, replace “server IP” with your server IP address.

If your server is responsive, the ping will be displayed at regular intervals.

When a query or task takes longer than expected, the server terminates it and returns a 502 Bad Gateway Error. It’s typical in shared hosting, and the provider disables the script to prevent it from causing problems for other websites on the same server.

Refresh the page

You may sometimes save yourself hours of debugging by just reloading the web page. That’s correct!

It’s possible that you’ll receive an overloaded server for a while and then it’ll come back on when you check back in a minute or two.

Clear Browsing Cache

The browser can cache or save website data so that the user does not have to re-download the same static resources every time they visit. It is a good strategy to use resources, but it does have certain disadvantages.

Assume you switched to an other browser and the web page loaded perfectly. You return to your default browser, knowing that your web page loads properly, yet the problem persists.

That’s because your browser is showing you a cached version of the website. Simply clear your browser’s cache to fix the problem.

Look for DNS problems

The Domain Name System, or DNS for short, is a method of connecting a domain name to an IP address. The 502 Bad Gateway Error might be triggered if the domain fails to resolve to the right IP address. This problem occurs while transferring a WordPress website to a new host, which necessitates DNS name server modifications.

It can take anywhere from 24 to 36 hours for the modifications to take effect. If you try to visit your site during this time, you may receive a 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.

Disable The CDN

Content Delivery Network (CDN) is commonly used to serve static website content. It is a useful method for delivering web material to a worldwide audience more quickly. However, due to an extra layer between the server and the browser, CDN could be one of the causes of WordPress 502 Bad Gateway Error.

Simply disable the CDN service and try to view your website again to eliminate this as a possible cause. You’ve found the culprit if the website loads properly. You simply need to contact CDN support, and if that is not possible, we recommend that you continue reading the rest of the post.

Audit Themes and Plugins

Any WordPress website would be incomplete without themes and plugins. A fully working website is practically impossible to achieve without the use of themes and plugins. They can also cause a 502 Bad Gateway Error in WordPress.

Incorrect PHP code in themes and plugins might cause issues that are either not understood by the server or take too long to process. A 502 Bad Gateway Error resulted as a result of this.

Begin by removing all of the plugins from FTP. Simply go to your root directory, i.e. the public html folder, navigate to wp-content/plugins, and rename the plugins folder to make it unavailable to the WordPress system.

Now, verify your website to see whether it loads properly, and if it does, start activating plugins one by one until you locate the one that is creating issues.

If you can’t locate any broken plugins, move on to looking for a faulty theme. Navigate to themes in your wp-content folder and rename your active theme to deactivate it. Removing it may not be the best solution, as re-uploading it may result in the loss of 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.

Check Logs

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. Users can take advantage of the integrated monitoring tool New Relic for precise and comprehensive analysis.

Timeout in PHP

When PHP hits the server’s maximum time limit for executing a script, it throws an error. It can sometimes result in a 502 Bad Gateway Error in WordPress. Simply increase the maximum execution duration value to avoid this. It is set to 300 seconds, which is 5 minutes, by default.

Examine the scripts

In WordPress, we design custom functions and themes. Your server may be unable to process them because of these custom functions. Calling third-party APIs that create long tokens, such as Google reCAPTCHA, can also place a strain on the server, resulting in a 502 Bad Gateway Error.

Last Thoughts

We hope that after reading this post, the 502 Bad Gateway Error will not be as frightening the next time you encounter it. If you can predict the source and follow the procedures provided in this article to correct it, troubleshooting is simple. We can still fix the WordPress 502 Bad Gateway Error by restarting the page and deleting the browser cache, but not every day is sunny, so be prepared.

Leave a Reply