502 Bad Gateway: How to solve it (2023)
Suppose you suddenly encounter a 502 Bad Gateway error while loading your website page or another website. In that case, the site’s server has likely encountered a problem, which can be caused by its lack of communication with the upstream server, server overload, or related problems. Configure the website. Your job as a server administrator is to find the cause as quickly as possible and take action to fix the 502 bad gateway error.
Error 502 is one of the most common errors of the 5xx series that originates from the website server and appears due to a problem in the communication between the server and the web server. Solutions such as refreshing the page or deleting browser cache and cookies are used to solve this error by the user, and solutions such as correct configuration of the web server, fixing PHP problems, and correct configuration of the firewall are used by webmasters to solve the error. Problems such as overloading the server permanently and server down are also problems that must be solved by hosting providers.
In simple terms, 502 is an HTTP status code that indicates that the web server in question has received an invalid response from another server, and this has caused you to encounter a white page with a 502 Bad Gateway error instead of the page contents when loading the page. If you, like most other users, do not know where to start to solve this problem, you are absolutely right because this error is a general and vague error with no fixed and specific reason.
However, in this article, we try to provide you with all possible reasons and solutions related to each reason so that you can solve this difficult and dumb puzzle as quickly as possible and continue your work.
Although this error is mostly related to server and server-side communications and configurations, this article will review solutions for website owners and website users so that you can easily complete this process.
If you also want to own a server with high security and power that minimizes the possibility of encountering such errors and provides your website to users as quickly and efficiently as possible, we recommend that you go to the virtual server purchase page. Apply, choose the most suitable option from the wonderful plans we have prepared for you, and make a strong start.
What is the 502 Bad Gateway error?
Every time you open your browser and visit a website, the browser sends your request to the site’s web server. After receiving and processing your request, the web server sends the requested resources with an HTTP header and the HTTP status code to the browser. Normally, when everything goes well, you should not see the status code, but if there is a problem sending and receiving the response, the probability that you will see the status code is very high.
If the error you encountered starts with 5, you can be sure that this error is related to the server and the communication between the web servers. Error 500 is the first error of this series, it happens due to problems such as misconfiguration of htaccess files and site database, and solutions to fix error 500 are effective to solve these problems.
Error 502 is one of the errors that show that the web server that acts as a proxy or remote server cannot communicate with the upstream server. In other words, the servers that communicate to display your website pages are unable to communicate. This can have many reasons, the most important of which are high website traffic or network connection problems. However, the result of this communication breakdown is a blank page with a 502 Bad Gateway error, which can appear in various forms and error texts:
The Reasons for the 502 error on the website
Although the error 502 is related to the server, if we go into the depth of the problem, there are many reasons that can cause this error:
Server overload
Issues such as excessive traffic or DDOS attacks can involve all the server’s resources, and as a result, the server will be disrupted due to the lack of resources. This is where the visitor cannot access the content of the intended page and instead faces a 502 Bad Gateway error.
Server failure
If the server’s hardware fails for any reason, any access to the website implemented on that server will certainly fail. Even, if the website server is under maintenance, the probability of encountering the 502 error is still very high.
Firewall oversensitivity
The firewall protects the site against traffic and unauthorized access. However, due to its high sensitivity, the probability of a bad gateway error 502 will increase if this firewall blocks internet providers and positive things.
DNS related problems
DNS is one of the important components of the Internet world that converts or translates the domain name to the IP address. Now, if this system translates the desired domain name to an incorrect IP address, there is a possibility of a 502 Bad Gateway error. For example, if you have recently transferred your domain name to another host, it can take 24 hours for the DNS server changes to fully propagate and take effect, and requests made during those 24 hours will encounter errors such as the 502 error. will be.
Network related problems
If your network connections are affected by problems such as router misconfiguration or loose physical cable, communication between servers will also be interrupted, and as a result, you will definitely encounter error 502.
Problems related to backend site programming
Sometimes, faulty programming can also prevent correct responses to users’ requests. Suppose your application is unable to process requests due to conflicting PHP scripts, an incorrect PHP version, or slow MySQL queries. In that case, the server will not be able to respond, resulting in a 502 Bad Gateway error.
Browser problems
In this case, it doesn’t matter if you are a visitor or the owner of the website, the browser version is old, the browser extensions are defective or the files in the browser cache memory can lead to the display of the 502 error.
Methods to solve error 502
Now that we are familiar with the main reasons for the 502 bad gateway error, it is time to examine the solutions to solve the 502 error in detail so that you can get through this stressful process as quickly as possible:
1. Refresh the page
Before you start looking at more complex and practical solutions, the best thing to do is to take a very short first step, and this first step is nothing more than reloading or refreshing the page. If your web server has been overloaded for a few minutes or witnessed a short wave of high traffic, the best thing is to wait a few minutes and then click the refresh icon next to the address bar or press the F5 or Ctrl button. + F5 (Windows users) or CMD + R (Mac users) Reload the desired page.
Repeat this several times, and if you still get error 502, try the following solutions.
2. Clear the browser cache
If outdated or corrupted information from your site is in the visitor’s or browser’s cache, a 502 error may occur. The best solution is to clear the browser cache.
However, keep in miache files may result in the loss of important browser data, such as bookmarks and preferences. To prevent this, it is recommended that you export your data from the browser first so that you can restore them later if necessary.
Steps to Clear Cache in Google Chrome:
- Click on the three dots in the upper right corner and select Settings.
- Go to Privacy and security and select Clear browsing data.
Check the two options cookies and other data sites and cached images and files and click on clear data.
Steps to clear the cache in Firefox
Click on the top 3 lines on the right and select Settings.
Select the privacy and security option and scroll down until you reach the Cookies and Site Data section.
Click on clear data, check the two options, cookies and site data and cached web content, and click on the clear button.
Steps to clear the cache in Microsoft Edge
Click on the 3 dots on the upper right and select Settings.
Click on the privacy, search and services option and scroll down to reach the Clear browsing data section.
Click on the Choose What to Clear button, then select the two options—cookies and other data sites and cached images and files—and click on Clear Now.
Steps to clear the cache in Safari
Click Safari in the top menu next to the Apple icon and select settings.
Select the privacy tab and click on the manage website data button.
Select the desired website or websites and click on remove and done respectively.
After you clear your browser cache, close and reopen the browser. Then, check if the desired page becomes accessible or if it still faces the 502 error.
3. Try Incognito mode
If you open the desired page in this mode and see that the page’s contents are available instead of the 502 Bad Gateway error, one of the browser extensions likely caused this error.
To ensure this, click on the top three dots and select the New Incognito Window option.
Then, open your desired page in this mode. If you don’t get an error in incognito mode, you need to find the plugin program causing the error.
Try disabling the plugins one by one until you finally identify the malicious plugin and remove it.
4. Try a different browser or device
If disabling plugins doesn’t help, you can try a different browser or even a different device. In general, these solutions aim to find and fix the source of the error.
So, try another browser first. For example, if you use Google Chrome, try another browser such as Firefox or Microsoft Edge. If the desired page opens without any errors, the problem is probably with the browser client, which is better to delete and reinstall.
If changing the browser doesn’t fix the 502 error, try another device, preferably connected to a different network.
This will help you determine if the cause of this error is related to the network equipment or if the problem lies elsewhere.
If you manage to solve this problem after trying a device with a different network, it is better to troubleshoot the network to which you cannot access the content of the page by connecting to. Restart the system and network equipment, unplug the modem you are connected to, and plug it back in after a few moments. Or if you are using phone data, turn data off until network-related issues are resolved.
5. Clear the DNS cache
As we already mentioned in the reasons section, DNS can also cause 502 errors. For example, if the DNS server cannot translate the domain to the correct IP address or is unable to respond to the request, the desired page will not be displayed for you.
The first thing you can do to solve this problem is to clear the DNS cache.
If you are a Windows user, just open CMD and run the following command:
ipconfig /flushdns
Correct execution of this command is accompanied by a successful message, “Successfully flushed the DNS resolver Cache”.
If you are a Mac user, open Terminal and run the following command:
dscacheutil -flushcache
Note that this command on Mac does not come with any output message.
If you are a Linux user, depending on your distribution, you can use the following commands:
sudo systemd-resolve --flush-caches
or
sudo service nscd restart
or
sudo systemctl restart systemd-resolved
If clearing the DNS cache doesn’t help, you can temporarily change your DNS.
6. Change your DNS server
DNS servers are usually set by the ISP, however, if you want to see if changing the DNS server is a solution to the 502 Bad Gateway error, you can do it yourself and change your dns server to another server such as Change OpenDNS or Google DNS.
The article on changing DNS settings can help you learn to change DNS on different systems and devices.
7. Check the server log files
Server reports provide important details about the status of the server, and by examining and analyzing them, you can get important information that may be the cause of the 502 error, waiting for you to find it.
Most web hosts provide a control panel that records detected errors in the form of an error report. If you have used WordPress to launch your website, adding the following lines to the wp-config.php file lets you activate the logging system and see all the errors generated from the wp-contents/debug.log file.
define( 'WP_DEBUG', true ); define( 'WP_DEBUG_LOG', true ); define( 'WP_DEBUG_DISPLAY', false );
8. Temporarily disable your firewall or CDN.
Today, many websites use CDNs and proxy firewalls to improve their performance and security. CDNs add an additional layer between the server and the browser, making web content available to users much faster. However, if this layer has a problem connecting to the main server, a 502 error may occur.
To determine if this error originates from the firewall or CDN, temporarily disable them and then check if you can access the content of the page or website.
9. Check out plugins and themes.
Sometimes, the plugins used in the WordPress website can cause a series of scripts and queries to be canceled by the server. Not up-to-date and defective plugins and themes can cause 502 Bad Gateway mistake, in such a case, you can check the status of your website by disabling them.
You have 2 ways to disable WordPress plugins:
If you have access to the admin dashboard, you can go to the Plugins section and check all plugins and then select Deactivate from the top menu.
Then, to find out which plugin is causing the 502 error, just activate them one by one and then check if you still have this error or not, until you finally find the malicious plugin.
Or, through ftp clients like Zilla, change the name of the plugins folder with another name like plugins_old. Then, check if the site issue is resolved or not. Then, if you find that the problem 502 is related to plugins, it is time to change the plugins_old folder to plugins again, and this time, change the name of the plugins in the plugins folder one by one until you find the desired plugin and delete the
10. Fix PHP issues
Some PHP issues can also cause error 502. The most important issues are long-running PHP code, conflicts between PHP scripts, and incorrect PHP versions.
PHP processes are executed according to the time limits set by the max_execution_time or max_input_time values. Max_execution_time corresponds to the maximum execution time of a script and max_input_time corresponds to the maximum input processing time. If the execution time of a script is more than max_execution_time, the Maximum execution time exceeded error will occur, which may also happen for input processing. These issues, in turn, can cause the 502 Bad Gateway error.
These times are set to 300 seconds by default, but you can change them by opening the php.ini file or asking your web host to increase them.
One of the common causes of PHP errors is an incorrect PHP version setting. In this way, you may have applied new updates or installed a new plugin and theme that is incompatible with the PHP version. In this case, the only thing you need to do is to change the PHP version and then check if this new version is compatible with the new updates of the website or not.
Of course, restarting PHP can also help solve problems related to the execution of PHP scripts, but this depends on the exact cause of the error and your server environment.
You can use different commands to restart PHP. For example, if you are using the Apache web server, run the following command:
sudo service apache2 restart
Or if you are using an Nginx web server, run the following command:
sudo service nginx restart
Of course, these commands are a temporary solution, and in order to identify the exact source of the problem, it is better to check the web server logs and PHP logs to reach a correct result.
11. Contact your hosting provider.
If you’ve tried all the solutions above and you’re sure your site and network connection are properly configured, it might be time to discuss the problem with your hosting provider. The hosting support team can gain a deeper understanding of the issues. By identifying a se by of clear symptoms, they can better sol. Theyr sitthen e’s 502 problem or provide you with the necessary guidance to do so.
What should I do as an Internet user to resolve the 502 Bad Gateway error?
If you have encountered a 502 error while visiting a website, it is likely that this error is related to the website server and has nothing to do with you.
The easiest thing you can do is to refresh the page, so if a temporary increase in server load is causing this error, refreshing the page can solve the problem.
If you still encounter this error after doing this, clear the cache and cookies of the desired website.
Trying another browser or device, updating the browser, clearing the DNS cache, changing the DNS, and loading the desired page in Incognito mode are all solutions that can help you find out whether the 502 problem is related to your browser or network connections.
If you still can’t solve the 502 error after doing these things, you can be sure that the problem is related to the server of the desired site.
What should I do as a webmaster to fix the 502 Bad Gateway error?
If you have found out through visitor reports or your own reviews that your website has encountered a 502 error, you should definitely take this problem seriously because not getting a response from users is equal to dissatisfaction a high bounce rate, and a negative result on the site’s SEO and ranking. Classification of site pages in search results.
First, you need to determine whether the problem is related to a specific page or the entire site.
If your server is down, contact your hosting provider. They may have discovered the problem sooner than you and are implementing a solution.
Problems such as incorrect configuration of the web server, excessive firewall restrictions, incompatibilities related to WordPress plugins, and disruption of CDNs are among the problems that must be solved by you.
But if you have done your own responsibilities to solve the 502 error, but you still haven’t succeeded in solving this error, it is possible that the server is down, the server is malfunctioning, the server is overloaded (permanently), the server is slow to respond, and other issues. This problem is related to the server. In this situation, the only thing you need to do is to contact the hosting provider so that they can do the necessary work to solve the problem.
Conclusion
The 502 Bad Gateway error is one of the most famous 5xx series errors related to server-side communications. Although an internet user can solve this problem by refreshing the page or clearing the browser cache, the main reason for this error is related to the server. The site’s webmaster or hosting provider should probably solve it.