Whether you’re visiting or running a website, you have likely run into the dreaded “502 Bad Gateway” error and possibly even wondered if there are ways to get around these issues or fix the error entirely. Depending on whether you are a visitor or a website owner, there may be several things you can try to resolve this issue. To begin, we need to understand what causes this error and the many possible ways to troubleshoot it before trying to fix the 502 Bad Gateway Error.
Table of Contents
What does error 502 Bad Gateway mean?
In a nutshell, this error comes from the web server of the website you’re trying to access. That web server is trying to get information from a different server, and a 502 error lets you know that something went wrong in the communication between those two servers. That error could indicate several issues, like the second server being unreachable or even an internal error happening inside that second server.
What can cause this 502 error?
More often than not, the server is at fault for this error. However, possible factors for seeing this error can include:
- Local Internet outage: Your ISP (Internet Service Provider) may have a temporary outage for any number of reasons, including maintenance, component failure, or even bad weather – each of which could affect your Internet access partially or completely.
- Bad DNS entries: There could be a change to the DNS (Domain Name System) entry for the site. This would make the website URL point to the incorrect server and display a 502 error.
- Connection timeout: Your request on the website may take so long to complete that the web server simply gives up and decides that the backend service is not responding.
- The website is overloaded: There could be too many visitors trying to access the site simultaneously, overwhelming the service and causing it to either drop connections or crash altogether.
This list actually extends beyond this, with factors that include the website server configuration, your computer’s DNS cache, and much more.
How do I fix the 502 Bad Gateway error?
This depends on whether you own the website or are just visiting it. As a visitor, you have no control over the server you visit, making it difficult to troubleshoot the cause, but there are a few things you can try and hope to get around the issue.
Fix the error as a website visitor
As a visitor, you still have options to rule out before concluding that the server is the problem.
1. Try to access the website on another device or with a website viewer service
The first issue to rule out is, “Am I the only one seeing this 502 Bad Gateway error?”. Try using a device different from the one you used to access the website initially, ideally one that can use a different Internet connection. A device like your smartphone would do well for this. If you can access the website, that points to the issue being with your device or your browser specifically.
Let’s say you don’t have such a device, or your other device also had the error. You can use a third-party service that can access any website from multiple geographical locations, such as GeoPeeker. Enter the website URL you want to test, and click “Go!”. This will give you the best idea of whether a website works for other visitors or not. If you see the same error again, it is a global issue.
Unfortunately, with globally unavailable websites that have the error 502 Bad Gateway, there is nothing you can do if you aren’t the website owner. The problem is between the web server and the service it is trying to access, so all you can do is wait for them to fix it.
However, if your computer alone is having trouble accessing the website, and other devices are able to access the website without issue, you can try the following methods
2. Try accessing the website using a different browser
First, try using a different browser, as that will allow you to preserve your cookies, cache, and logins. Download another browser, such as Mozilla Firefox or Google Chrome. You can also try the browser that comes included with your computer, such as Safari or Microsoft Edge. If you can access the website using any other browser, you will likely need to clear your browser data in your main browser and try again. Clearing your browser data and cache is fairly straightforward, but the exact steps depend on the browser you’re using.
3. Clear your browser’s data and cache
This might be worth trying as there may be bad cache data preventing your browser from loading the website properly. You can clear your browser data and cache on various browsers by doing the following:
Google Chrome:
- Click on the three vertical dots found at the top right of your browser.
- Click on More Tools.
- Click on Clear Browsing Data.
- Set the time range to “All time”.
- Check the checkboxes for “Cookies and other site data” and “Cached images and files”.
- Click on “Clear data”.
Microsoft Edge:
- Click on the three horizontal dots found at the top right of your browser.
- Click on Settings.
- Click on “Privacy, Search, and Services” in the left sidebar.
- Scroll down and click on “Choose What to Clear”.
- Set the time range to “All Time”.
- Check the checkboxes for “Cookies and other site data” and “Cached images and files”.
- Click on “Clear Now”.
Mozilla Firefox:
- Click on the three horizontal lines found at the top right of your browser.
- Click on Settings.
- Click on Privacy & Security in the left sidebar.
- Scroll down and click on “Clear Data…”.
- Ensure both boxes are checked and click on the “Clear” button.
Safari:
- In the status bar or menu bar of your Mac, click on the “Safari” menu item found at the top left of your screen.
- Click on Preferences or Settings. Click on the Privacy tab -> Click on “Manage Website Data…” -> Find the website you want to clear the data for and click on “Remove” or simply click on “Remove All”. Click on Done when you are finished.
NOTE: This will sign you out of most websites, requiring you to sign in again. This can also slow down your website experience temporarily, as all remote content has to be downloaded again.
Try accessing the site once again. If you are able to access it now, then great! The cause was likely a caching issue.
If you’re still seeing the 502 Bad Gateway error, you can try to…
4. Change your DNS provider
DNS is the system that connects IP addresses, the addresses that define the location of a computer or server on the Internet, to domain names, the URL that you type in your web browser. Your computer has to have a DNS server set in order to be able to connect to websites on the Internet. Sometimes, this DNS setting might get stuck or point to an old IP address where the site is no longer located. Changing the DNS server can sometimes help resolve a 502 Bad Gateway error.
How to change the DNS Server on Windows 11:
- Open the Settings app.
- Click on “Network and internet” in the left sidebar.
- Click on Ethernet or Wi-Fi, depending on what you are using to connect to the Internet.
- In the “DNS Server Assignment” section, click on the Edit button.
- In the dropdown, select Manual.
- Type in a DNS IP address.
Changing the DNS Server on macOS Ventura:
- Open the System Settings app.
- Click on Network in the left sidebar.
- Select the network connection that says “Connected”.
- Click on “Details…”.
- Click on DNS in the left sidebar.
- If there is an IP address present in the section under DNS Servers, select it and click on the minus button to remove it.
- Click on the plus button and type in an IP address.
DNS IP addresses to try: 1.1.1.1
or 8.8.8.8
are the most popular and common DNS servers to use. Try switching to whichever one your computer was not using, or try either one out. If it helps, then likely a DNS entry was not updated yet on your existing provider.
5. Flush your DNS cache
If you changed your DNS server and still have trouble, it is possible that your computer has cached DNS entries to help speed up Internet browsing. You can clear this by running one of these commands, depending on your operating system:
Clear your DNS cache on Windows:
- Go to your start menu and open a command prompt. (cmd)
- Type this exact phrase:
ipconfig /flushdns
- Press [Enter] on your keyboard. That should clear your DNS cache.
Clear your DNS cache on macOS:
- Open Finder, go to Applications > Utilities, and open the Terminal app.
- Run this exact command:
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
- Your computer will ask for a password. Enter your login password and press [Enter] on your keyboard. Your DNS cache is now cleared.
Try to load the webpage again now. Your computer will contact the newly configured DNS server for the latest entry. If you are still having trouble, it is possible that the DNS entries set by the website owner are configured incorrectly, in which case the only fix would be to try again later.
6. Check and disable any browser extensions
You may have some extension in your browser that redirects the connection to a server with the error. It’s worth disabling any extensions that could affect your browser connections and trying again.
7. Restart your device
A last resort option is to try and restart your computer. This might seem not to do much. However, a restart does reset your Internet connection and some networking caches.
Fixing the error as the website server owner
If you run the website and you can verify this issue using more than one device, then your server is the issue. Specifically, the connection between your web server and the backend service you are using is not connecting for a reason. Here are some items you should check. Note that our suggestions are for a server running a Linux-based operating system.
1. Check whether your backend service is running
The most common cause of the error 502 bad gateway error is your backend service is not running. This problem can be verified by checking whether the port that the service listens on is closed or open. You can check which ports are open on your device using this command :
sudo netstat -tunlp
You may first need to install the net-tools package using your system’s package manager. Of course; you need to know which port your service was running on first. If your service is not present in the list, this means that your service is down, which is causing the 502 error. Start your service using systemd or any custom command used to run your service. Try to open the website after 30 seconds or so. That should hopefully fix the issue. If you’re still getting the 502 Bad Gateway error, you need to…
2. Check your firewall rules
If your service is running and you still experience the 502 bad gateway error, the next logical step is to check your server firewall.
The method of checking your firewall depends on your firewall program. We recommend trying with the iptables command first. This is almost always the underlying technology for firewalls on Linux.
iptables -nvL INPUT –line-numbers
This command will list all of the incoming firewall rules with numeric and more verbose output. Again, this requires that you know the port number that your service is using. If you see an entry that mentions your server IP or 0.0.0.0/0, together with your port, and it says DROP under target, that is a rule that could be blocking access to your backend service.
It’s possible that you accidentally added a rule to try and protect your backend service. However, you blocked everything out by mistake instead of allowing only your server to connect.
Either way, this should be straightforward to fix. Find the line number that the rule is on (the line number is at the far left), and use iptables to remove the line:
iptables -D INPUT <line number>
This command will remove any rule found on the line number you enter. After removing the rule, try to access your website.
Still not working? Try this next potential solution.
3. Check your timeout rules for your reverse proxy
Depending on your web server settings, you may have a reverse proxy timeout that is too short for the requests you are trying to make. This is usually the case on slower servers that have a slow CPU or slow storage. One option is to upgrade to a better hosting provider with faster storage and CPU, allowing your server to perform more requests in less time.
The other option is to increase the timeout limit of your web server. Here’s how to do that:
Increase reverse proxy timeout limit for Nginx:
proxy_read_timeout 300; proxy_connect_timeout 300; proxy_send_timeout 300;
Increase reverse proxy timeout limit for Apache:
ProxyPass /your/path https://yoururl.com connectiontimeout=300 timeout=300
The values across both examples are set to 300 seconds, which is 5 minutes.
4. Disable your CDN or clear its cache
If you are using a CDN, it is possible that a user received this error through your CDN, and the CDN then cached your page with the error code, showing it to everyone afterward. The best way to fix this problem is to clear the cache. Once you have cleared it, you can try reloading the page.
Did that help? If not, you can try this next fix.
5. Increase your PHP execution timeout
If your website uses PHP, you can try to increase the execution time of your PHP pages. Since PHP is run on the server side when a page is requested, something may be causing the page processing to get stuck. Whether that’s a connection that can’t be made, or a slow server taking too long to render the webpage, increasing the execution timeout can help.
To do this, edit your PHP configuration by opening your php.ini file. You can find it using this command:
php -i | grep php.ini
The output of this command will give you the location of the file. Open the file with your preferred PHP editor (we prefer nano) and find these configuration lines:
max_input_time = 300 max_execution_time = 300
This is measured in seconds. Set it as long as you reasonably think it should take for a script to be executed.
If you are using PHP-FPM, make sure to restart the service in order to read the configuration change.
If this solution was not helpful, you can try this next fix too.
6. Ask your hosting provider for assistance
If you’ve tried all of these tips and are still unable to solve the problem, you might need to get in touch with your hosting provider for additional help. Don’t forget to tell them everything you have tried already. For example, describe what you changed and where it was changed.
If you use a Managed Linux Hosting provider, they will be able to try to debug and solve the problem for you. If you have an unmanaged provider, it’s likely that you’ll be on your own and be left to figure it out by yourself.
Wrapping Up
Now you are better aware of what the 502 Bad Gateway means – a problem between the web server and any service it tries to connect to. More often than not, the user or visitor has nothing to do with the error, but it can happen in some cases. Either way, hopefully, these suggestions have been helpful in getting you back on the website you want to or getting your website back onto the Internet.
Did this article help you fix your website or computer? We would appreciate you spreading the word to your friends or colleagues by sharing this post. Thank you!