how to fix 502 bad gateway

502 Bad Gateway Error: What Is It And How To Fix

Websites are getting bigger and bigger day by day. As it gets larger, its maintenance becomes more complex. That’s why, every now and then, errors occur. For easy identification, all the errors have a number. Here we will talk about how to fix the 502 bad gateway.

Usually, we may encounter a series of errors while using websites. Some are very common, like 404, and some might be a bit uncommon, like 101. These are called the HTTP status codes.

HTTP status codes 

1xx – Informational responses 
2xx – Successful responses 
3xx – Redirection messages 
4xx – Client error responses 
5xx – Server error responses 

As we can see from the list, the 502 error is some kind of server error. So, let’s start with what Bad Gateway 502 means.

What is the 502 bad gateway?

The HTTP 502 bad gateway is a communication error between two servers. Here, one server acting as a gateway or proxy receives an invalid response from the upstream server.

502 error variations

The HTTP 502 bad gateway has different variations. That means 502 can show different messages depending on the reason.

Error Code
502: Bad Gateway Error
Error Type
Server-side error
Error Variations
502 Bad Gateway
Error 502
HTTP Error 502: Bad Gateway
502 Service Temporarily Overloaded
502 Proxy Error
A blank white screen
502 Server Error: The server encountered a temporary error and could not complete your request.
HTTP 502
Temporary Error (502)
502. That's an error
502 bad gateway Cloudflare
Bad Gateway: The proxy server received an invalid response from an upstream server.
Error Causes
Server overload
Browser issues
DNS issues
Web host issues
Firewall blocks
Plugins and themes errors

What causes 502 error?

The 502 bad gateway error occurs for any server or server-related issue. Let’s dive a little deeper into what’s causing the error.

Server overload or timeout:

Servers can be overloaded due to too many requests and high traffic. The server can also fail and take too much time to process a request. During this time, the proxy or gateway server can show the 502 error.

Network issues:

As we know, the error occurs due to a communication interruption. So, the network can cause the error. A network failure can occur on the server, the proxy, or maybe both and cause the error.

Proxy server configuration:

A proxy server helps you prevent cyberattacks. But if the proxy server is not configured properly or faces any issues, a 502 error will occur.

Server downtime:

The server can be down for any technical reason or for maintenance. So, if the upstream server is down, the client will see the 502 error.

Application or web server errors:

Web server applications such as Apache, Nginx, or database applications like MySQL can fail to process server requests and can show the error 502 Bad Gateway.

Firewall or security software:

From time to time, the server’s firewall or security system can block the communication temporarily due to any threat detection. This can cause a 502 at the client end.

How to fix the 502 bad gateway error?

If this error occurs on your site, there are a few things you can do to fix it.

1. Refresh the page

Sometimes the error can occur due to a sudden hike in traffic on the server. In that case, we can just wait a few minutes and refresh the browser. Most of the time, this will revive the site.

You can try a hard refresh to make sure the cache is also cleaned for that site. On Windows, it’s CTRL+F5, and on Mac, it’s CMD+SHIFT+R.

2. Clear browser cache

If the hard refresh doesn’t fix the issue, then the browser might save some corrupted or outdated cache. In that case, we have to clean the browser cache to solve the 502 error.

You have to be careful before cleaning up the cache. Because it might clear important browser data like history, bookmarks, and preferences. But you can specify what you want to delete and what not. From browser to browser, the settings can vary.

3. Try in incognito mode

If cleaning the cache from the browser does not fix the 502, then it may be some browser extension. You can confirm this hypothesis by opening the site in incognito mode.

By default, incognito mode does not have any extensions enabled. But if you have enabled any extension for the incognito mode, you have to disable it first.

If the 502 error is coming from any browser extension, then your site will load in incognito mode without an issue.

4. Test on another device

If the issue still exists, then try a different device. You can use your phone, preferably by connecting it to mobile data.

If the site doesn’t show the error on your phone, then we will go for the classic fix. Restart the device and also restart the WiFi router. This is to fix a possible network issue that might cause the error.

5. Flush DNS servers

The domain name system, or DNS server, can cause the 502 error. Sometimes the DNS server can be unresponsive to a request, or it might be misconfigured. Cleaning the DNS server can fix these issues. This issue is very common if you transfer the site to a new host.

For a faster response, DNS server information is stored locally. It’s called a local DNS server. Issues can also be triggered locally as well. You can also clean the local DNS server on your device.

Open your terminal on Windows or Mac, and use the below command to flush the local DNS server.

Windows

ipconfig /flushdns

Mac

dscacheutil -flushcache

6. Check error log

Sometimes, changing something on your site can cause an error. In that case, you can go through the error log to find out what went wrong. On WordPress, you can use plugins like Error Log Monitor to check the error list.

You can also enable the WordPress native error log. You can go to the wp-config.php file and set “true” for ‘WP_DEBUG’.

wp_debug code for 502 bad gateway fix

7. Check plugins and themes

Speaking of WordPress, plugins and themes can cause 502. If the installed plugin or theme is not verified or has any bugs, the server firewall can block the communication, which results in an error.

You can easily go to the WordPress dashboard and check the installed plugin list. If you find anything suspicious, you can uninstall it. If there is nothing suspicious, then simply check them one by one and see which one is causing the error.

WordPress Plugin causing 502 error
Fix 502 by changing WordPress Theme

The same goes for the WordPress theme. Go to appearances and see the list. Try to activate a different theme and check if the issue is still there or not.

8. Check CDNs

The content delivery network, or CDN, is another server between your browser and the main server. CDN basically decentralises your site’s content to different geographical locations. So, 502 errors are very common on the CDN, just like on servers.

To confirm the hypothesis, you can simply disable the CND and see if the site is loading or not.

9. Check firewall

The server’s firewall can kick in and go to defensive mode if it faces any threat. The first thing any firewall does is disconnect the communication. And this can cause the error.

To test this assumption, you can disable the firewall and see if the site loads or not. To fix 502 bad gateway errors, you have to check for threats first. If there are no threats, then you can update the firewall rules.

10. Check the site status

Let’s say you have tried most of the methods and failed to solve the issue. Then there is a good chance that your server might be facing downtime.

You can check if the server is down or not using tools like isItDown rightNow and Down For Every One Or Just Me. These can help you find out if the server is actually down or not.

If this is the case, then you have to communicate with your hosting provider to take the necessary steps to fix the error.

11. PHP time out

If any of your PHP processes take a longer time than the max_execution_time or max_input_time, then 502 might occur. This is a common issue during a big site migration.

This time-out limit is set for the web server in your PHP configuration. Generally, it is set to 300 seconds. If you need more time, then contact your hosting provider and request to elevate the value.

12. Contact customer hosting support

If nothing works, then there might be some major issue on the web server end. You have to contact your hosting provider’s customer support to look into the issues.

502 can affect site SEO

If your site is down, it will impact your site’s traffic. And not to mention, all the work for SEO will also be affected.

Google crawl: If your site is showing 502 for a long time, say more than 6 hours, or it happens frequently, then it will be a big issue. If Google starts to crawl and sees the 502 error multiple times, it will drop the ranking of your site.

Loss of authority and increased bounce rate: If your visitors continuously get 502, then they might not return to your page again. That way, the click-through rate will decrease. Because of the 502 error, the bounce rate will increase and your authority will drop.

Final thoughts

We always have to be careful before making a website. From choosing a hosting platform and CMS to installing plugins, anything can cause an error. But it doesn’t mean it’s the end of the world. There is always a fix for any issue.

So, we now know how to fix the 502 bad gateway. And we hope this has solved your issues. Happy browsing.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *