What is 502 bad gateway error and how to solve it?

What is 502 bad gateway error and how to solve it?

As you know, in order to see a website, your browser has to send several requests to different servers, which then send you the content it is asking for. If something goes wrong during this process, the server sends an HTTP status code to the browser so that you can know that something has gone wrong and, based on the error, get a rough idea of ​​what it is. The 502 bad gateway error, along with the 404 status code, is one of the most common that you will find on the net.

How can you solve it quickly if it is your website that is affected?

What is error 502 Bad Gateway?

The 502 Bad Gateway Error, like all HTTP status codes that start with the number 5, occurs because there is an error caused by an item from outside your computer or browser, not necessarily caused by the end server. Specifically, the 502 Bad Gateway error tells you that a server that was acting as a gateway or proxy has received an invalid response from an incoming server. The display of error 502 may vary slightly depending on the server in question. It may appear with expressions similar to: “Error 502”, “502 Invalid Gateway”, “502 Bad Gateway”, or “502 That’s an error ”, etc.

What Causes Error 502 Bad Gateway

As I mentioned before, Bad Gateway errors are caused (in general) by a communication problem between the origin server and one of the servers that act as a gateway. The server acting as the gateway or proxy has tried to access the main server to respond to your request, but has either encountered an invalid response or has had no response.

The most frequent culprits for error 502 are the following:

  • There is a firewall blocking the request, that is, the communication between the perimeter servers (those that serve as a proxy) and the origin server.
  • The proxy server that has received your communication is not able to route it to the final server.
    Your request goes through a very large cluster (like the ones on Twitter or Reddit that I mentioned before) and you have the bad luck that your request goes to an end server of the group that, at that moment, is out of service.
  • Do you use a CDN? In that case, your server may be failing for another reason and may not be able to handle your requests.

Keep in mind that these types of responses can be cached, so it is possible that the fault will be seen even if it is already solved. In this case, pressing Ctrl + F5 and reloading the page is enough to refresh the cache and fix the problem (or not, in which case you will have to check the server-side cache).

In case the problem is not so temporary and persists, here are other possible solutions.

How to fix error 502 “Bad Gateway”

There are several ways you can try to get to the website you want to visit after receiving a 502 Bad gateway error, but until you try it you won’t know which one will work for you. First of all, I recommend that you check if the website has fallen or if it is only you who cannot enter. Keep in mind that if it is down for everyone and you are not the site administrator, the only thing you will be able to do is wait and try again later. To quickly find out if the server is accessible, you can do a ping test or a traceroute.

If the website affected by error 502 is yours and you want to be more aware in the future, you can configure a monitoring service such as Pingdom (for a fee) or Uptime Robot (free). These tools check the availability of your website every few minutes and will notify you if for some reason they cannot access it.

Beware, monitoring services can give false alerts from time to time, so don’t be alarmed until you see for yourself the alleged problem.

Having said this, we will go at once to the possible solutions that you can apply if the error 502 appears on a website that you are interested in accessing. From easiest to most complex, here you have them!

Method 1: Reload the page

It is possible that the server that hosts the web is overloaded for some reason (for example, due to a peak in requests). Press Ctrl + F5 or the Refresh button to reload the page and check if you can already access it. If not, close all browser tabs and reopen it, wait a minute, and try to access the web again.

Still nothing? So try using a different browser. If you can access it normally, it is best to uninstall the failed browser and reinstall it again. Now, if the failure persists even from another browser, go to the next method to fix error 502.

Method 2: Clear the browser cache

Sometimes, your browser will simply remember that previously this website resulted in an error. To make sure that the error you are receiving is current and not a mere “reminder” and that your browser is not storing outdated or corrupt information, clear your browser’s cache and cookies. If the problem was this, deleting the cached files and reloading the page will fix error 502 and you can access the web.

Method 3: Deactivate the CDN

The CDN or content delivery networks ( Content Delivery Networks ) are used to improve the loading speed of websites. However, they can sometimes cause the 502 Bad Gateway error because they use additional firewalls. To check if this is the case, temporarily deactivate your CDN and check if the error has disappeared.

An example of the most used CDN is Cloudflare since it has one of the best free plans. If it is the network you use, it will be very easy to know if it is the culprit of error 502 because on your screen you will see something like this:

Method 4: In WordPress, check plugins and theme

WordPress plugins or themes may have flawed code, leading to gateway errors, and causing unexpected issues on your site.

Try disabling the plugins one by one, checking in each case if the error 502 disappears. If you’re using a cache plugin, start with that one: it’s your prime suspect.

Deactivating plugins is easy via the WordPress dashboard’s Plugins section, requiring no complex steps or barriers. Instead, if you cannot enter the administration area, follow these steps:

  • Enter the wp-content folder and rename the Plugins directory to “Plugins-disable”. What you will cause is that all the plugins are deactivated at the same time. When the site functions error-free, it signals a problematic plugin causing the 502 error.
  • Rename the folder again, from “Plugins-disable” to “Plugins” and enter it.
  • Deactivate the plugins one by one. To do so, change the name of each plugin, for example, change “jetpack” to “jetpack-disable”. After each change, check if the error 502 has disappeared or if you can still see it.

Deactivating the plugin causing a 502 error will restore your website’s functionality, resolving the issue and ensuring proper activation. Leave the plugin disabled and find out what has happened: is it misconfigured? Has it been updated for a long time? You may have to find an alternative plugin.

Method 5: Check the error logs

Recent alterations on your site might lead to error 502 showing up as a potential outcome. To check if this is the case, the easiest way is to check the error log. The log’s location differs based on hosting; exact guidance isn’t possible due to varied setups. However, you can create a phpinfo.php file that includes this bit of code:

<?php
phpinfo();
?>

Put it in your root folder and access it from your browser using the URL www.yourdomain.com/phpinfo.php. Look for the text error_log and there you will have the path where your error log is located.

If you use cPanel, you will find the error_log file if you follow these steps:

  • Access the control panel, go down to the Files section, and click on File Manager.
  • On the left side, click on the public_html folder, and inside it, you will find the error_log file.
  • Logs for subfolder access (“yourdomain.com/folder”) are in public_html/folder within your page’s directory structure.

This log records site errors by age, detailing reasons and specific error lines for each occurrence.

If you use WordPress, you can enable error logging by adding this code to the wp-config file :

define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );

In general, the generated entries are usually stored in the wp-content directory.

Method 6: Restart backend services

This is a bit technical, I’ll warn you! For web servers using PHP daemons like php-fpm or php-lsapi, if unresponsive, your service may be down or inaccessible. Using Nginx to direct traffic to a busy or down Apache server can cause similar issues.

Restart the final service like Apache or php-fpm; if down, it won’t respond, causing a 502 proxy error. Remember to check that there is no firewall rule preventing your proxy from communicating with Apache or PHP! Firewalls aren’t just for external threats; internal connections can also face vulnerability.

[Also Read: How to Fix YouTube Audio Renderer Error?]

Conclusion

Troubleshooting unclear errors is a webmaster’s frustrating challenge, pinpointing issues amidst uncertainty. Therefore, a basic understanding of the specific error will greatly advance you in the course of diagnosis. As you can see in this article, there are several possible ways to fix the 502 Bad Gateway error.

Have you understood what error 502 consists of and the possible solutions that can be applied? If you have any questions, leave a comment and I’ll try to help you. Do you think I have forgotten something or do you have more ideas to diagnose and fix a 502 error?

admin

Tech Crazee is a website came up with a great content on all multiple niche like business, technology, finance and more. Tech Crazee studies, analyze's and presents before publishing in this website. We the Tech Crazee team established a platform to build a proper and trustful medium with the internet world and the people.