blog posts

502 Bad Gateway: How to solve it (2023)

502 Bad Gateway: How to solve it (2023)

If you have suddenly encountered a 502 Bad Gateway error while loading your website page or another website, it is likely that the site’s server has encountered a problem, which can be caused by its lack of communication with the upstream server, server overload, and 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 one of the general and vague errors that do not have a fixed and specific reason.

But in this article, we try to provide you with all possible reasons and solutions related to each of these reasons, 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 the server and server-side communications and configurations, in this article we will review both solutions for website owners and solutions for website users so that you can go through this process easily.

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 and choose the most suitable option from among the wonderful plans that we have prepared for you and make a strong start.

Table of Contents

  • What is the 502 Bad Gateway error?
  • The reasons for the 502 error on the website
  • Methods to solve error 502
  • What should I do as an Internet user to resolve the 502 Bad Gateway error?
  • What should I do as a webmaster to fix the 502 Bad Gateway error?

 

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, and after receiving and processing your request, the web server sends the requested resources with an HTTP header. and sends 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 in the process of 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 is responsible for protecting the site against traffic and unauthorized access. Now, if this firewall blocks internet providers and positive things due to high sensitivity, the probability of bad gateway error 502 will increase.

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 due to problems such as misconfiguration of routers or loose physical cable, communication between servers will also be interrupted and as a result, encountering error 502 will be a definite thing.

Problems related to backend site programming

Sometimes, faulty programming can also prevent correct responses to users’ requests. If your application is unable to process requests due to conflicting php scripts, incorrect php version or slow Mysql queries, the server will not be able to respond to requests and the result will be nothing but 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 then if you see that you are still facing error 502, try the following solutions.

2. Clear the browser cache

If the cache of the visitor’s browser or your own contains a series of old and faulty information from your site, there is a possibility of a 502 error. The best idea to solve this problem is to clear the browser cache.

Of course, note that targeting cache files can lead to the loss of important browser data such as bookmarks and preferences, so to avoid that, it is better to export your data from the browser first so that you can restore them later. import

Steps to clear the cache memory in Google Chrome

Click on the 3 dots on the upper right and select Settings.

Click on privacy and security and select the clear browsing data option.

 

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 and 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 on Safari located 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 instead of the 502 Bad Gateway error, the contents of the page itself are available, it is likely that one of the browser extensions caused this error.

So, to make sure of 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, it’s time to find the plugin program that is causing this 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, the goal of these solutions is to find the source of the error and fix it.

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 solve the 502 error, it’s time to 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.

To learn about the steps of changing DNS in different systems and devices, you can get help from the article on how to change DNS settings.

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 you with a control panel that records detected errors in the form of an error report. If you have used WordPress to launch your website, by adding the following lines to the wp-config.php file, you can 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 the performance and security of their websites. CDN adds an additional layer between the server and the browser so that web content is available to users much faster, but if this layer has a problem when connecting to the main server, there is a possibility of a 502 error.

To find out if this error originates from the firewall or CDN, you can disable them temporarily 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 issues related to PHP can also lead to error 502. Long-running PHP code, conflicts between PHP scripts, and incorrect PHP versions are the most important issues.

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 if needed, you can open the php.ini file and change these values or ask your web host to increase these values.

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 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 have a deeper view of the issues and by reaching a series of clear symptoms, they can better solve the 502 problem of your site or provide you with the necessary guidance to solve it.

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 after doing this, you are still facing this error, 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 of all, you need to check whether the problem is related to a specific page or the entire site.

If your server is down or down, you should contact your hosting provider. Maybe they discovered the problem faster 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 you, as 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 and should probably be solved by the webmaster of the site or the hosting provider.

Thank you for staying with us until the end of the article. We hope that reading this article was useful for you. If you have any questions, or requests and need guidance, you can contact us by registering your opinion so that we can answer you as soon as possible.