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

What is error 502 bad gateway 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 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 take to 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 trace route .

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 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 be simply remembering 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:

502 Bad Gateway

Method 4: In WordPress, check plugins and theme

It is possible that some of the plugins or the theme of your WordPress contain faulty code (or are incorrectly configured) and, as a consequence, they are triggering gateway errors that should not be occurring.

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.

To deactivate the plugins, you will not have any difficulty as long as you have access to the WordPress dashboard and the Plugins section . 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. If the web works and no longer gives 502 error, it is clear that one of the plugins is to blame for the 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.

At some point, the plugin that you deactivate will be the culprit of error 502 and your website will be correctly activated again. Leave the plugin disabled and find out what has happened: is it misconfigured? Has it been updating for a long time? You may have to find an alternative plugin.

Method 5: Check the error logs

If you have made any changes or updates to your website recently, it is possible that error 502 is a consequence of this. To check if this is the case, the easiest way is to check the error log . The location of the log varies depending on the web hosting and the control panel that you have hired, so I cannot give you specific instructions. 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.
  • eye! If you were accessing a subfolder on your page (for example, “yourdomain.com/folder”), the logs will be in the public_html / folder directory .

This file will contain, organized by age, all the errors that have occurred on your website, including the reason for the error and the line on which it occurred.

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! If you use a web server configuration in which you invoke a PHP daemon – as can be the case with php-fpm or php-lsapi – it is very possible that your service is down or unresponsive. This can also happen if you use a reverse proxy type configuration ; for example: you have an Nginx web server that routes requests to an Apache that is very busy or, in the worst case, it has broken.

In these cases, it is imperative to restart the final service (Apache, php-fpm, etc.) because if it is down it will not respond to your proxy and, if your proxy does not receive a response, it will return a 502, which is the most literal example of this error.

Remember to check that there is no firewall rule preventing your proxy from communicating with Apache or PHP! It is easy to think that the firewall is only for external connections, but those that are internal can also be affected.

conclusion

Resolving an error when you are not clear what is causing it is one of the most frustrating processes a webmaster has to deal with. 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 the 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?

Tech Crazee

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.

Leave a Reply