The 502 Bad Gateway error represents an HTTP status code indicating that a web server got an incorrect response from the originating server. This type of error, which is displayed in the browser window as web pages, has absolutely nothing to do with the way your computer has been configured. This means that you can see it on any browser, operating system or device.
It is not uncommon to see when loading a page a number of error codes called status codes, clearly telling us that a page is not available or to wait a moment or even click on a link to be redirected.
Among these codes is the 502 Bad Gateway error, which often prevents Internet users or webmasters from accessing the content or the query they are looking for. It is obviously not a fun game for those who encounter this kind of error.
You may have already been subject to this one, and you may not have known how to react, how to correct it or how to simply bypass this type of error to get your request answered.
As you can understand, there is a very specific way to deal with the 502 Bad Gateway error, and that is what we will see in this article by providing answers to these questions:
- What does a 502 error mean?
- What causes it?
- What can it look like?
- How to fix the 502 Bad Gateway error?
Follow this article carefully so that you don’t get caught by this category of errors again. Let’s start!
Chapter 1: 502 Bad Gateway Error: What is it?
Bad Gateway errors are server errors frequently encountered by Internet users and owners of WordPress websites.
In this chapter, I will explain what a 502 error is, what it looks like, what causes it, and what impact it has on SEO.
1.1. what does 502 Bad Gateway error mean?
In order to better define what a 502 Bad Gateway error is, it is first important to become familiar with the concept of servers, and to understand the meaning of certain terms such as
- Web server;
- Proxy or gateway server;
- And upstream, origin or base server.
Let’s start by looking at how a modern web application is hosted. For this fact, it is necessary to have three elements:
- The web application;
- The gateway;
- The web server.
In the image below, you can see the process that a modern PHP (Hypertext Preprocessor) web application follows.
A web application is an application software that can be coded in several different programming languages and can use frameworks or specialized libraries.
It often has mechanisms to handle HTTP requests. In the case of your wordpress website, the web application may correspond to your wordPress website, which is coded in PHP.
The gateway is located between a web server like Nginx or Apache and a web application. Its role is to receive requests from a server (upstream or origin) and then interpret them for a web application. The term gateway is not very easy to define.
Some call themselves process managers, others call themselves HTTP servers.
The common characteristics of a gateway are as follows:
- Receiving requests (HTTP, FastCGI, uWSGI, etc.);
- Translation of requests into application code;
- Creation of multiple processes;
- Controlling the created processes;
- Distribution of requests between processes;
PHP-FPM or PHP-FastCGI Process Manager is the gateway to PHP. It is an implementation of FastCGI that listens for FastCGI requests from a web server.
FastCGI is a binary mechanism for connecting interactive programs to a web server. The CGI (Common Gateway Interface) is a technology and a web program that describes how a web server can communicate with external applications, including PHP.
The CGI is therefore an intermediary between the web server and the web content that is produced by web applications developed in various programming languages, such as PHP or Python. FastCGI is just an optimized version of CGI.
As far as the web server is concerned, it has several functions:
- It hosts multiple sites;
- It provides static files;
- It transmits requests to other servers;
- It performs load balancing;
- It ensures the caching of HTTP.
It should be mentioned that the two most famous web servers are Apache and Nginx. In the past, Apache was the most used web server before Nginx became more common.
Now that the terms mentioned below are well understood, you will finally understand what a 502 Gateway error is.
1.1.1. What is a 502 Bad Gateway error?
A 502 Gateway error occurs when the web server behaves like a gateway server and gets an invalid response from the upstream server. A 502 Bad Gateway error means that the proxy server representing the gateway server fails to get a good result from the upstream server (base server).
If you notice a 502 Bad Gateway error, it means that there is something wrong with the upstream server. There are a number of reasons for this, which we will see in the following paragraphs.
1.1.2. The various types of 502 Bad Gateway errors
There are several types of web servers, operating systems or web browsers used by users. For example, you may be using the Google Chrome browser and macOS. At the same time, other people may be using Firefox and the Windows 10 operating system.
Even more, some websites are able to customize the form that the 502 Bad Gateway error should take, when it is displayed to users.
However, these different variants have the same meaning, which states that a server that served as a gateway or office got an invalid response from an origin or upstream server.
Here are some of the most common ways in which you can see the 502 Bad Gateway error:
- 502 Bad Gateway;
- 502 Server Error: The server encountered a temporary error and could not respond to your request;
- HTTP 502;
- 502 Error;
- Temporary Error (502);
- 502 Temporarily Overloaded Service;
- 502. Error;
- 502 proxy error ;
- 502 bad Cloudflare gateway ;
- 502 HTTP error – bad gateway ;
- 502 Bad Gateway NGINX.
Some platforms or other services may choose different ways to display the 502 error. Twitter, for example, shows a completely different message that says “Twitter is overloaded”. That’s all we know.”
The Google search engine also shows it in a completely different form. If using its browser, you come across this error, you can see a message that says:
“502. This is an error. The server encountered a temporary error and could not respond to your request. Please try again in 30 seconds. This is all we know.“
1.1.3 Impact of Error 502 on your website’s SEO
When error 502 becomes common on your website, it can negatively impact your website’s SEO. Indeed, this error affects the reputation of your web page, both for Internet users and for search engines.
In the end, this leads to a bad experience for consumers insofar as it is even preferable, to the one who manages a site that presents errors so often, to abandon its role.
Because, if this error remains for a long period of time on your site, there is a strong probability that the site will be completely de-indexed from search engines.
This is for the ultimate reason that this site is no longer able to provide data to Internet users, which will cause an increase in bounce rates and a drop in rankings.
With this detrimental impact that this error can have on your site, you are surely wondering what could actually be the source of it. Here are the probable causes of a 502 error.
1.2. Causes of a 502 Bad Gateway error
502 errors are caused by the fact that the server was unable to provide a valid response to the request made. At least that is what has been said so far. But you should know that there are other reasons for this type of error as well.
Here are the most common causes:
- The base server is down;
- The domain name;
- Request blocked by the firewall;
- Server failure;
- Browser problem.
1.2.1. The originating server is down
One of the reasons you may encounter a 502 error is that the server is down or not working properly. This would also mean that the server is not accessible
This situation can be explained in several ways:
- Either the server is down;
- Either there is a connectivity problem;
- Or the traffic on the server is very high.
1.2.2. The domain name
The 502 Bad Gateway error can also be explained by the fact that the domain cannot determine the correct IP address. This problem is often caused by incorrect DNS records at the domain’s hosting.
Even if the DNS records you have set are correct, it should be remembered that DNS changes take time to propagate to all levels of the Internet. This time may depend on a number of factors including lifetime or TTL.
1.2.3. Query blocked by the firewall
The firewall allows to block any form of transmission between servers. It is another common reason why you are confronted with the 502 error
It can happen that the security plugins of WordPress :
- Prevent communication between different servers;
- Or block your local IP address or that of your ISP.
To avoid this possibility, you can try to visit the site using a different Internet connection.
Also, some DDOS protection systems may be too sensitive to block server requests.
1.2.4. Server failure
The other cause of encountering a 502 Bad Gateway error is when the server fails completely. This can be due to a system malfunction or sometimes the server is taken down for maintenance, etc.
This failure can also be caused by content that appears to violate the terms and conditions of the server provider.
1.2.5. Browser-related problem
It is important to know that some browser extensions can also cause errors when loading the website, especially AdBlock extensions.
Apart from that, there may be rare cases where the error is due to an outdated browser version.
Now you know what a 502 Bad Gateway error is, its causes and its impact on SEO. Let’s see the different methods to be used to remedy it.
Chapter 2: Methods to troubleshoot a 502 Bad Gateway error
Several techniques are used to repair 502 Bad Gateway errors that occur. When talking about these methods, I will have to present them in two categories:
- Those that concern the user;
- And the ones that the developers have to implement.
2.1. Methods used in the event of a Bad Gateway error for users
2.1.1. Refresh the page
When you are faced with a 502 error, the probability is very high that the website server is overwhelmed by a volume of traffic than it can usually handle.
If you are in this case, you can try to solve the error by refreshing the page. To do this, simply click Ctrl + R or F5 on your keyboard to reload the page.
This error may be just a temporary one that occurred during the period when the server is subject to heavy traffic or a possible DDOS attack.
If you try to refresh the page again a few minutes later, you may not see this error anymore.
Source : wpoven
2.1.2. Try in private browsing mode (incognito)
Another way to solve this error is to start a new private or incognito session on your browser. All you have to do is close the existing tabs and then start a brand new private or incognito session.
If the error occurred because of a problem with your browser, restarting your browser in a private session may solve the problem.
2.1.3. Clearing the browser cache
There may be instances where corrupted files stored on your browser may be the source of the 502 Bad Gateway error. Therefore, if you delete the data from your browser’s cache, this error can be solved.
On the other hand, I also suggest you to clear your browser’s cookies. This might also help to fix this error.
2.1.4. Test other browsers
There are several other browsers you can try, if the one you are using is giving you a 502 error. Try another browser like:
- Chrome ;
- Safari ;
- Fireforce ;
- Internet Explorer
If the error no longer appears when you switch browsers, it probably means that your old browser is the root of the problem. It is also possible to reinstall the browser to see if the problem is solved.
2.1.5. Restart your network device
Restarting your network device can also resolve a 502 Bad Gateway error. All you need to do is stop the connection to your internet modem. To do this, you need to unplug your internet modem by holding down the button for 10 seconds.
Once these signals are completely turned off, you need to plug it back in, activate the power button and wait until your computer can establish a new connection.
2.1.6 Clearing the DNS cache
The 502 Bad Gateway error is able to occur due to DNS malfunction. You can see operating systems like Linux, Windows or macOS, make the storage of data related to name resolution in the form of DNS cache.
In many cases, deleting the DNS cache can remedy a 502 Bad Gateway error. Below are the various commands you can use to delete the DNS cache on Windows, MacOs and Linux.
To delete the cache on Windows, use the following command:
For macOS, you need to open the terminal and type :
sudo killall -HUP mDNSResponder
No message is displayed after this command, but you can add your own by doing the following command
sudo killall -HUP mDNSResponder; dns cleared successfully
The situation is different in Linux, because the various Linux distributions use different DNS services. These include :
- NSCD (Name Service Caching Diamond) ;
- Dnsmasq ;
- Bind (Berkeley Internet Name Domain).
Concerning the NSCD DNS cache:
sudo /etc/init.d/nscd restart
In the case of a dnsmasq DNS cache :
sudo /etc/init.d/dnsmasq restart
For BIND DNS caches, here is the command to enter:
sudo /etc/init.d/named restart
sudo rndc restart
sudo rndc exec
In case the terminal asks you for a password, just enter it.
You can also temporarily change your DNS servers.
2.1.7. Methods to use if you use Cloudflare
When faced with an origin server that sends a classic HTTP 502 Bad Gateway error message, Cloudflare displays a 502 error of its own: 502 bad gateway appearance.
This means that there is something wrong with your origin server. Here are the tips to follow, to fix this problem.
When the 502 error comes from Cloudflare, the page looks like this:
Source : 10 weblog
If the word “Cloudflare” appears on the error, it means that the server comes from Cloudflare, otherwise it comes from the original server. At first glance, it is advisable to refer to Cloudflare’s helpdesk, or choose to follow the tips described. If no positive changes are seen, get in touch with your hosting provider.
You have just seen some tips that will allow you to fix a Bad Gateway error on the user side. Now follow what you can do on the developer or server side.
2.2. Methods to use in case of a 502 Bad Gateway error for developers
Are you a developer or a server manager and you are confronted with this error? There is a high probability that it is related to factors other than what we have seen before.
Here are some recommended methods to solve this problem.
2.2.1. Examine the server and DNS
One way to determine whether or not your server is accessible is to perform a trace or ping test on the server’s IP
You can also check if your full domain name is set or not. As mentioned above.
2.2.2. Checking error logs
Another possibility for developers is to check the error logs of the server or the site created on it, to see if a specific error is reported on the server. If you are using WordPress, you can add these different codes to your wp-config.php file.
Source : Wproven
By doing this, you can have all the data related to errors that occurred from any newly added application. You will also see all the elements that have caused errors that your site has produced.
2.2.3. Examine your WordPress plugins and themes
The presence of unoptimized code in WordPress plugins and themes can also cause 502 Bad Gateway errors. Therefore, I advise you to check your WordPress themes and plugins.
If you have access rights to your WordPress administration, try to deactivate all your plugins. If the error disappears after this process, it means that there is at least one plugin responsible for the error.
So start sorting out each plugin by activating them one by one to find the offending plugins. If you don’t have access to the WordPress administration because of the error, but you can access your WordPress files, simply rename the plugins folder in wp-content.
This will deactivate all the plugins, then resume their activation one by one. If deactivating all the plugins or renaming the plugins folder doesn’t change anything, try momentarily changing your theme to the default WordPress theme.
After finding the failed plugins or theme, contact the support team and explain your situation.
Above all, keep in mind to always update your plugins, theme or WordPress engine. This will help you avoid many problems including, 502 errors.
Follow our article on how to install a theme on WordPress
2.2.4. Check your Firewall
You can also check your firewall history for any unusual blockages. A misconfigured firewall can cause a 502 Bad Gateway error
A firewall is a network security system that monitors the flow of traffic in and out of the network according to predefined security rules.
It can happen that some inadequate firewall configurations consider compliant and safe content as malicious, therefore blocking the traffic, which in turn causes 502 Bad Gateway errors
To overcome this problem, you should temporarily disable the firewalls to see if the problem persists or has been resolved.
2.2.5. Temporarily disable the CDN
The other reason that can cause the 502 error is the fact that your server is subject to a DDOS attack. In this case, you can use services like Fail2ban to try to lock down the offending IP addresses.
You can also use CDN providers like Cloudflare, to minimize any kind of DDOS attacks targeting the server.
2.2.6. Restarting the PHP-FPM service
It is important to know that a 502 error can be displayed when the PHP-FPM service is not active or not working on the server. If you have access to hosting, it is possible to check this by using one of the following commands.
For the SysVinit command
sudo service php7.4-fpm status
For the SystemD :
sudo systemctl status php7.4-fpm
If the PHP-FPM service is active and functional, the result of the command will look like this
Source : 10web
If on the other hand the status indicated is not: “active” (running), it is advisable to try to restart the PHP-FPM service in order to repair the error with the following commands.
In the case of SysVinit :
sudo service php7.4-fpm restart
In case of SystemD :
sudo systemctl restart php7.4-fpm
2.2.7. Faults relating to the timeout
Error 502 is sometimes caused by a timeout of PHP-FPM. If the application takes a long time to give a response, users may see a timeout error.
If the PHP-FPM timeout is shorter than the Nginx timeout, Nginx will display a 502 Bad Gateway error. To avoid this problem, it is possible to increase the timeout of PHP-FPM, if you have access to the server.
The timeout of PHP-FPM is set in the pool settings, namely
PHP runtime errors can also be the cause of 502 Bad Gateway errors. To remedy this, simply increase the PHP configuration parameters, such as max_execution_time and max_input_time.
If you can access your server, you can simply change these directives in your php.ini file. Otherwise, it is possible to ask your webhost to take care of the task.
Now you don’t have to panic anymore when you get a 502 Bad Gateway error on the way. The latter is an error related to a transmission problem between servers. It is mainly a status code that means that the proxy server cannot get a valid response from the base server.
There are several possible causes for this error. It can also have a negative impact on the SEO of your website. This is why it is ideal to take into account immediately this error especially when it becomes recurrent.
It is in this perspective that I had to present to you in this article, the different methods to exercise to solve this error, as well in the developer as in the user.
I hope this article was useful to you. I look forward to your comments.