Support

Find answers, guides, and tutorials to supercharge your content delivery.

How to Solve 502 Bad Gateway Issues?

Updated on April 29, 2022

If you browse the internet long enough, you'll run into a 502 Bad Gateway error code. This is especially true if you frequently visit websites known to be unreliable. While the error can be frustrating, the good news is that it's usually easy to fix.

In this article, we'll take a look at what a 502 Bad Gateway error code is and how you can go about solving it on your own.

What does 502 Bad Gateway mean?

A 502 Bad Gateway indicates that the edge server (server acting as a proxy) could not get a valid or any response from the origin server (also called the upstream server). This can occur for a few reasons, which we'll discuss below. If one of KeyCDN's edge servers receives a 502 Bad Gateway response from your origin server, KeyCDN will return a 500 Origin Not Reachable page.

To better understand how KeyCDN handles certain error codes, check out our CDN Error Codes page.

Usually, a 502 Bad Gateway error code is accompanied by a message letting you know that a "bad gateway" exists. In other words, something went wrong while the server was trying to relay information to another server (usually an upstream server). The most common cause of this issue is an incorrect or outdated DNS record.

What are the reasons for 502 Bad Gateway responses?

Three main culprits cause 502 Bad Gateway responses. These include:

  1. Domain name not resolvable: The domain name is not resolving to the correct IP or it does not resolve to any IP. It is important to note that DNS changes could take same time until they are global fully propagated and active. This is dependent on the TTL, or time to live, defined per record.
  2. Origin server down: The server is not reachable, either because it is down or there is no connectivity to the server given.
  3. Firewall blocks request: A firewall blocks the communication between the edge servers and the origin server. This can also be caused by security plugins of your CMS. Some DDoS protection and mitigation systems might are too overreactive and start blocking requests from our content delivery servers.

How you might see a 502 Bad Gateway error

Based on your web server, you might see a different 502 error.

Every time, it means the same thing. The only different component is their naming conventions. Here are a few examples of what you might see:

  • 502 Bad Gateway
  • HTTP Error 502 - Bad Gateway
  • 502 Service Temporarily Overloaded
  • Error 502
  • 502 Proxy Error
  • HTTP 502
  • 502 Bad Gateway NGINX

You can see in greater detail what the error specifically entails by going to your web server's error log file. All error and diagnostic information is stored in this file making it a valuable resource to check when you need more details about a particular error. You can locate this file in Apache by going to /var/log/apache2/error.log and in Nginx by going to /var/log/nginx/error.log.

You can see what the error specifically entails by going to your web server's error log file in greater detail. All error and diagnostic information are stored in this file, making it a valuable resource to check when you need more details about a particular error. You can locate this file in Apache by going to /var/log/apache2/error.log and in Nginx by going to /var/log/nginx/error.log.

How to solve 502 errors as a web developer

As a web developer or owner of the website, there are a few reasons why you may be experiencing a 502 Bad Gateway error on your origin server. Therefore, you may need to try various methods to resolve the issue. Reference the list of suggestions below:

  • Check if your FQDN (fully qualified domain name) is resolving correctly by using our DNS test tool.
  • Verify if your server is reachable by using a ping test or traceroute.
  • Check your firewall logs if you are seeing unusual drops.
  • If you're a Cloudflare user, try disabling it as once you've reached a certain limit Cloudflare will return a 502 Bad Gateway error to your visitors.

How to solve 502 errors as a visitor

If you're a website visitor and experience a 502 Bad Gateway error then there is also a few things you can try to resolve it. Although the primary issue will almost always be the responsibility of the web developer, visitors can try the following:

  • Perform a hard refresh in your browser. On Macs, this is done by pressing Cmd + Shift + R.
  • Clear your browser cache and delete cookies. Your browser may be holding on to certain files that were saved once you visited the website with a 502 error.
  • Restart your computer/networking equipment
  • Change your DNS servers. If you've never changed them in the past you likely still have the default servers assigned to you by your ISP, try using open DNS servers such as Google's Public DNS.

Common solutions for 502 Bad Gateway Error Code

Usually, a 502 error code is the result of a server error. A server error means that the server you are trying to access is unavailable or down. However, it is also possible on the client-side.

Here are some common ways to fix it in detail:

Reload the page

The first and easiest thing you can do is simply reload the page. A 502 error code and a 504 error code are both server-side errors. This means that there is something wrong with the server. The problem could be anything from the server being down to the server being too busy.

A reload will send a new request to the server. The server may be down, but it could also be that the original request timed out. A reload will often fix the problem.

Tools like DownForEveryoneOrJustMe are useful for seeing whether this is a server-side issue or an issue on your end.

Clear your browser's cache

It is also possible that your browser's cache is causing the problem. If you have recently visited the site, your browser may be trying to use an old version of the website. To clear your browser's cache, go to the settings in your browser and look for the option to clear your cache.

In Google Chrome, you can go to the three dots in the top right corner and click More tools. From there, click Clear browsing data.

In Mozilla Firefox, you can go to the three bars in the top right corner and click Options. From there, click Privacy & Security and scroll down to Cookies and Site Data. Click Clear Data.

In Microsoft Edge, you can click the three dots in the top right corner and click Settings. From there, click Choose what to clear under Clear browsing data.

In Safari, you can click the Safari menu and click Preferences. From there, click the Privacy tab and click Manage Website Data. Click Remove All.

Restart your computer and router

It is also possible that the problem is on your end. If you have recently installed new software or made changes to your computer, these could be causing the problem. Try restarting your computer and see if that fixes the problem.

If the problem is on your end, one of your browser's extensions is likely causing the problem. Try disabling all of your browser's extensions and see if that fixes the problem.

If the problem is still not fixed, you can try resetting your router. To do this, unplug your router from the power outlet and wait 30 seconds. After 30 seconds, plug it back in and wait for it to connect to the internet.

Change your DNS servers

If you are still having problems, you can try changing your DNS servers. If you have never changed your DNS servers before, you likely have the default ones assigned to you by your ISP. If your DNS server is not responding or your domain is not resolving the correct IP, it can result in a 502 error.

However, you can try using public DNS servers such as Google's Public DNS.

To change your DNS servers in Windows, go to the Control Panel and click Network and Sharing Center. From there, click Change adapter settings and double-click your internet connection.

Click Properties,then click the Networking tab. From there, double-click Internet Protocol Version 4 (TCP/IPv4).

In the General tab, click Use the following DNS server addresses. Enter 8.8.8.8 for the Preferred DNS server and 8.8.4.4 for the Alternate DNS server.

Click OK to save your changes.

You can also change your DNS servers in your router's settings. The process will vary depending on your router, but you will likely be able to find the DNS settings in the Network or Internet section of the router's settings.

Once you have changed your DNS servers, try loading the website again.

Flush DNS cache

DNS caching can also cause 502 errors. When you visit a website, your computer stores certain information about that site in its DNS cache. This can help your computer load the website faster the next time you visit it. However, if this information is outdated, it can result in a 502 error.

To flush your DNS cache, open the command prompt and enter the following:

Windows: ipconfig /flushdns

Mac: sudo killall -HUP mDNSResponder

Linux: sudo /etc/init.d/networking force-reload

Once you have flushed your DNS cache, try loading the website again.

Temporarily disable Your CDN or firewall

Most servers use a content delivery network (CDN) such as KeyCDN. While very helpful (and important), these services can sometimes cause 502 errors. If you are using a CDN, try temporarily disabling it and see if that fixes the problem.

To disable your CDN in WordPress, go to the CDN tab in the Settings section of your WordPress dashboard and click the Disable CDN button. Click Save Changes.

In other CMSs, the process will be similar. Look for a CDN or Content Delivery Network settings page in your CMS's dashboard and follow the instructions.

Similarly, your firewall could also be the cause of 502 errors. If you are using a firewall, try temporarily disabling it and see if that fixes the problem.

To do so, go to the Control Panel and click System and Security. From there, click Windows Firewall.

Click the Turn Windows Firewall on or off link in the left sidebar. If prompted for an administrator password or confirmation, enter the password or provide confirmation.

Click the Turn off Windows Firewall (not recommended) option for both private and public networks and click OK.

Summary

If you see 502 Bad Gateway errors, there are a few things you can do to try and fix the problem. Usually, this error code is an easy one to fix and requires one of the following:

  • Reload the page
  • Clear your browser's cache
  • Restarting your computer or router
  • Flushing your DNS cache
  • Temporarily disabling your CDN or firewall

If you are still having problems, you can try changing your DNS servers. If you have never changed your DNS servers before, you likely have your ISP's DNS servers configured. However, you can try using public DNS servers such as Google's Public DNS.

Worst case scenario, you will have to contact your ISP's customer support. They should be able to help you troubleshoot the problem and get you back online.

KeyCDN uses cookies to make its website easier to use. Learn more