What is a 502 Bad Gateway Error, and how to fix it?

Andrew Moore
Updated: January 17, 2023

Not everything goes smoothly when actively surfing the web. Users encounter HTTP errors instead of website pages from time to time. If you encounter a 500 error, the problem is not always related to your Internet connection but to the website server. The 502 error is an exception, indicating incorrect settings, especially if the error window frequently appears regardless of the site you are trying to access. The methods of solving the 502 Bad Gateway error are different for a user and a site administrator. Let’s take a closer look at the causes of this error and the ways to fix it.

What is a 502 Bad Gateway Error

502 Bad Gateway – what does it mean?

The HTTP Internet Protocol responds to each request with a status code. The most common one is 200: OK (everything works as it should). This status is not usually displayed. Instead, visitors see the content of the site. There are different causes of different errors: while errors 400 mean that the user has a problem, codes from 500 to 511 mean that a negative response is the result of a server error. The numbers also indicate where the error occurred, at least for the users who know what unique codes mean.

The 502 error is usually accompanied by phrases such as Bad Gateway, Bad Gateway Nginx, Bad Gateway Apache, or Bad Gateway: Registered endpoint failed to handle the request. This error of the wrong gateway and the “400: Page not found” is the most common error when working on the Internet. When the site was accessed through the central server, it failed to forward the request. In this case, the first server functions only as a proxy or gateway. All submissions on the Internet are also forwarded through gateways. This is why an error window can cause inconvenience: it is not entirely clear at what point of the process the request detected a failure.

Sources of the 502 Bad Gateway error

In most cases, the wrong gateway error occurs on the server, so an ordinary user cannot fix it. The browser you are using may also register the 502 Bad Gateway error, but it does not exist, or you are just disconnected from the network. There may be the following sources of the error:

  • Server failure: the gateway receives a negative result from the target server. This can happen due to a system failure. In rare cases, servers may be disconnected from the network (for example, when content violates the law);
  • Web server overload: if a web server reaches its limit, it can no longer respond to requests. That’s why a gateway issues the 502 Bad Gateway error. It can be caused by an unexpectedly higher interest in the site or even a DDoS attack. This means that a hacker uses the virtual network to overload the server and provoke a system failure;
  • Incorrect PHP programming: sometimes, the site code contains the problem. In this case, some requests receive negative results;
  • Connection error: errors caused by firewalls (server and client) may cause errors in forwarding requests on both sides of the connection;
  • Browser error: your browser extensions can cause a website display error and the 502 Bad Gateway error.

Causes of this error

There are three leading causes of the 502 Bad Gateway error:

  • The domain name by IP address is not resolved. DNS changes can take as long as they are active. This depends on the TTL of a “lifetime” of each record;
  • The source code is unreachable: there is no Internet connection;
  • A firewall blocks the request: it blocks communication between the edge and the source servers on the site. CMS security plugins can also cause it. Some DDOS protections are too active to block content delivery servers’ requests.

How to fix the 502 Bad Gateway error?

Let’s consider solving the 502 Bad Gateway error as the site administrator. Suppose a visitor to your portal has shared that they see a 502 error message when trying to access your site. What does this mean to you? An administrator cannot always determine the cause of the error correctly. First, you need to check if the entire site is accessible. If your server goes down, you should contact your hosting provider. They may already know about the error, and their support is already working on the recovery. You should also check your server log files for error messages. PHP programming is often the source of errors, so check if the source of the error is in the code or the database. If necessary, you can use an old backup to fix the error quickly.

The error often occurs just because the server was temporarily overloaded or took too long to respond to a request. If such overloads arise more often, you should contact your hosting provider. You can achieve a better web server configuration by working together, but it is not always possible. Then you should check if your firewall is configured correctly and ensure it is not blocking any legitimate requests. Faulty name resolution may be another source of the error. Check the DNS settings at your hosting provider. The 502 error often disappears on its own. When the error occurs without your impact, it is resolved by your Internet Service Provider (ISP) or web server hosting provider. If you are responsible for the problem, you need to determine the source of the error and the ways of fixing it as soon as possible.

Temporarily disable your content delivery network (CDN)

Browser requests are sometimes redirected through a reverse proxy server. In other words, using a proxy server creates an intermediary between the user’s browser and the site server. Some services, such as content delivery networks (CDNs), use reverse proxy technology to route incoming traffic as efficiently as possible. But sometimes, this extra layer can cause problems when connecting to the original website server. When this happens, you and visitors to your site may encounter the 502 error. It is easy to check and determine if your CDN is causing this problem. All you have to do is temporarily disable the service and then check if the site is loading correctly without it. The process of undermining the CDN depends on the service you are using.

Now, when you try to access the website, its source server responds to your request without intermediaries. If error 502 disappeared, your CDN was probably the source of the problem. In most cases, you just need to wait a few hours before enabling the service again. Make sure that the error no longer occurs. If you use another CDN, review its documentation for instructions on temporarily disabling the service.

Clear the browser cache 

If the problem is related to the server, but you need to access the site’s content, there are ways to solve the problem. The browser cache is one of them. Modern browsers store copies of websites on their servers for a while so they can display earlier versions. To load a page from the cache of Google:

  1. Type cache: in the search box and the URL you want to visit.
  2. Keep in mind that you are viewing an outdated version of the site.
  3. Clear your browser cache to get rid of the error.

Delete your browser cookies

In most cases, you can fix error 502 by refreshing the page. If the problem resolves, then the server is just temporarily overloaded. Since such crashes can last long, refreshing the page may help in about 15 minutes. If the error message persists, clear the cache and cookies. The browser might just access data from the internal memory of your device. You don’t have to delete all cookies. Open and delete cookies related to the appropriate website in the browser settings.

Run your browser in incognito mode

The error may be in the system even if the status code indicates something else. If Bad Gateway pops up every time you try to visit any site, the error is related to your computer, browser, or firewall. First, check the functionality of your browser by restarting it in incognito mode. If this does not help, run the browser in safe mode. If the error disappears, then one of the browser add-ons or extensions disabled in safe mode caused the problem. You need to open the command prompt and type the full path of the browser location in quotation marks. Each browser developer has different commands for starting safe mode:

  • Firefox: “C:\Program Files\Mozilla Firefox\firefox.exe” -safe-mode;
  • Internet Explorer: “C:\Program Files\Internet Explorer\iexplore.exe” –extoff;
  • Chrome: Google browser does not offer a safe mode. It is enough to run in incognito mode instead.

Fix DNS server

Error 502 may occur because the domain name and IP address are mapped incorrectly due to a website migration to a new host. You can solve the problem by clearing the DNS cache on your local system the way you clear the cache in your browser. Open the command prompt and run this command: ipconfig / flushdns.

Check logs

Logs are records of all the actions you perform in any application. You can check the application logs and the corresponding log on the server. The application logs will tell you what pages were requested during the work of the application. In contrast, the server log contains information about the hardware responsible for executing the application and the status of any devices connected to the server.

Disable the CDN plugin

The Content Delivery Network (CDN) is a third-party service for rendering “heavy content,” including large images and videos. CDNs are used by the application to maintain efficiency. Firewall configuration may allow invalid or malicious content to pass through the gateway, resulting in the 502 Bad Gateway error. You just need to disable the CDN to solve this problem.

Try using another browser

Try to connect using another browser. If the page opens smoothly, the current default browser should be uninstalled and reinstalled. If switching browsers does not solve the problem with the wrong gateway error, you need to check your network settings. Restarting your modem and router is the easiest method. If that doesn’t work, try switching from Wi-Fi to LAN and using a cable connection.

Restart your computer and network equipment

Sometimes the 502 Bad Gateway error is caused by your ISP. A false entry on their DNS server is the reason for the problem. Enter another DNS server in the settings to bypass the error message. You can do this in the TCP/IP connection settings in Windows. As a test, you can access Google’s free DNS server, such as 8.8.8.8 or 8.8.4.4.

Contact your Internet Service Provider 

If your ISP does not cause the error, you just need to wait until it disappears or use cached versions of pages. In most cases, the error is temporary and can be resolved by the administrator as soon as possible.