What is a 502 Bad Gateway?
The 502 Bad Gateway status code indicates the gateway or proxy server received an invalid or faulty response from one of the servers trying to fulfill your request.
Multiple servers sometimes work together to fulfill a request. The initial server you connected to acts as a gateway server, making it the intermediary between you and the other servers containing the requested resource.
When one of these servers cannot complete the request, maybe because it is down, misconfigured, or experiencing connectivity issues, the gateway server returns a 502 Bad Gateway status code.
The 502 Bad Gateway error belongs to the 5xx series of HTTP status codes.
5
indicates there is an issue with the server or its configurationxx
is a placeholder for two numbers that provide more information about the server error
Causes of the 502 Bad Gateway Error
The 502 Bad Gateway error could be caused by several reasons, including:
Browser extensions – Browser extensions can interfere with web requests and block the browser from communicating with the server.
Website coding errors – Poorly written or buggy website code can prevent the server from processing requests or cause it to crash.
Firewall blocking access – Firewalls can incorrectly block legitimate traffic and disrupt servers from communicating with one another.
Incorrect proxy settings – Misconfigured proxy settings can prevent the proxy server from properly forwarding requests to the destination server.
Server overload – When a server is overwhelmed with excessive traffic or requests, it may struggle to respond or crash, leading to a 502 Bad Gateway error.
Network problems – Issues with the network connection can disrupt the communication between the browser and the server.
DNS issues – Problems with the Domain Name System (DNS) can prevent the browser from connecting to the correct server.
Server software timeouts – If the server software takes too long to respond, it may result in a timeout and result in a 502 Bad Gateway error.
Bad code or scripts – Errors in the website’s code or scripts can impact the server’s performance and lead to a 502 error.
Server misconfigurations – Incorrect server configurations or settings can cause the server to respond with a 502 Bad Gateway error.
Variations of the 502 Bad Gateway Error
Site owners can customize the 502 Bad Gateway error. So, the exact message displayed differs from site to site. Some sites may even display a white screen without any error message.
However, some common 502 error messages include:
- 502 Bad Gateway
- HTTP Error 502
- Error 502
- HTTP 502
- Bad Gateway
- HTTP 502 – Bad Gateway
- Temporary Error (502)
- 502 Server Error
Sometimes, the error may include the name of the server from which the error originates. So, you may encounter error messages like:
- 502 Proxy Error
- 502 Bad Gateway Apache
- 502 Bad Gateway NGINX
- 502 Bad Gateway Cloudflare
Some servers may also display messages that provide information about the source of the error. For example:
- 502 Service Temporarily Overloaded – Indicates the server is overloaded.
- 502 Bad Gateway. The server returned an invalid or incomplete response – Indicates the server received a response it cannot process.
How to Fix the 502 Bad Gateway Error
The 502 Bad Gateway error could be caused by several factors, so you may have to attempt several solutions to resolve it. However, it is good practice to try the basic solutions first before moving on to the more complex ones.
If you are a visitor to the site, you can attempt to:
- Refresh the page
- Crosscheck the URL
- Use a private window
- Use a different browser
If you own the site or have administrator access to it, then you can try to:
- Review your error log
- Update your theme and plugins
- Disable your theme and plugins
- Inspect your proxy server
- Disable your firewall
- Review your Content Delivery Network (CDN)
- Increase your PHP execution time
- Flush the DNS cache
- Contact your hosting provider
1 Refresh the Page
Refreshing the page is one of the simplest and quickest ways to resolve a 502 Bad Gateway error. This especially works when the error results from a brief glitch in the server connection.
In this case, the error is temporary and can be easily resolved by clicking the refresh button on your browser. However, if the page still returns an error, you may have to look into other solutions.
2 Crosscheck the URL
The 502 Bad Gateway error can occur when the URL is incorrectly typed or contains extra characters that the server cannot process. In this case, review the URL to ensure it is correct and points to an existing resource.
3 Use a Private Window
Private windows, also called incognito windows, disable your extensions and prevent the browser from using cached data or stored cookies.
So, open a private or incognito window on your browser and enter the URL returning the 502 error. If the website loads correctly in incognito mode, the issue may be related to your browser’s cache, cookies, or extensions.
4 Use a Different Browser
Browser settings, extensions, and cached data can prevent a site from loading correctly. So, switch to another browser and check if you can access the site.
If the site loads correctly on a different browser, then the error may have originated from the browser or its extensions. In this case, clear your browser’s cache. If the error is not resolved, disable your extensions one after another to find the one causing the error.
5 Review Your Error Logs
Error logs provide detailed information about technical issues on your site. So, connect to your site via an FTP client or log into your web hosting control panel.
- Nginx servers typically store logs in
/var/log/nginx/error.log
- For Apache servers, the error logs are typically located in the
/var/log/apache2/error.log directory
If you are using WordPress, you might also find error logs in the wp-content
directory.
Look for entries that correspond to the time when the 502 error occurred. These entries can provide insights into what went wrong. You can then proceed to resolve the issue.
If the logs indicate a timeout or connectivity issue, it might suggest problems with the server’s ability to communicate with other servers. In such cases, checking the server configuration and network settings can help resolve the issue.
6 Update Your Theme and Plugins
Outdated themes and plugins can cause compatibility issues, especially when used with an updated version of WordPress. You can resolve this by heading to your WordPress dashboard and updating your themes and plugins to the latest version.
7 Disable Your Theme and Plugins
Themes and plugins can conflict with one another or with the server settings, leading to the 502 Bad Gateway error.
You can resolve this by deactivating all your plugins and switching to a default WordPress theme like Twenty Twenty-Four. If the website loads correctly after deactivating the plugins and themes, the issue likely lies with one of them.
To identify the problematic plugins or themes, reactivate the theme and then the plugins, one by one, while checking the site after each activation. Pay special attention to recently updated or newly installed plugins and themes, as they are often the underlying cause.
8 Inspect Your Proxy Server
Proxy servers act as intermediaries between your computer and the internet. However, misconfigurations or connectivity issues between them and other servers can lead to a 502 error.
You can rectify this by checking the proxy settings in your browser and network configuration to ensure they are correctly set up. If you have a manual proxy configuration, confirm its proxy address and port are correct.
If your proxy settings are correct, disable the proxy and check if the error is resolved.
9 Disable Your Firewall
Firewalls are designed to block malicious traffic from your site. However, they can also block legitimate requests, leading to 502 errors.
You can resolve this by disabling the firewall on your computer, router, or network security application, depending on where it was configured. After disabling the firewall, try accessing the site again.
10 Review Your Content Delivery Network (CDN)
Content Delivery Networks (CDNs) help cache your site on multiple servers across the world. This allows you to deliver your site to visitors quicker, irrespective of how far they are from your server.
However, connectivity issues between your server and CDN can result in 502 errors.
To resolve this, check with your CDN provider to ensure that there are no disruptions from their end. Then, log into the CDN to confirm that your server’s IP address is correctly configured.
You should also review the CDN logs to identify any issue that may have caused the error. As a last resort, you can disable your CDN and test whether your site returns the error.
11 Increase Your PHP Execution Time
Increasing your PHP max_execution_time
and max_input_time
can help resolve 502 Bad Gateway errors that occur due to scripts timing out before completion.
To do this, navigate to your site’s root folder and check for the php.ini
file. Then, locate the max_execution_time
and max_input_time
directives and increase their value.
You might change them to 300 seconds. Remember to restart your server after making the changes.
max_execution_time = 300 max_input_time = 300
12 Flush the DNS Cache
Outdated and corrupted DNS information can cause communication issues that lead to the 502 Bad Gateway error. Fortunately, you can resolve this by flushing your DNS cache.
You can flush the DNS cache using the command prompt on Windows or the terminal on Linux and macOS.
- For Windows, open the command prompt, type
ipconfig /flushdns
, and press Enter - For macOS, open the terminal, type
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
, and then press Enter - On Linux, the command may vary, but a common command is
sudo systemd-resolve --flush-caches
This process clears the DNS cache, allowing your computer to retrieve the most recent DNS information from the DNS server.
13 Contact Your Hosting Provider
When other troubleshooting methods fail, contact your hosting provider. Your hosting provider has access to the server logs, advanced diagnostic tools, and technical expertise that can help identify and resolve issues causing the 402 Bad Gateway error.
You should contact your hosting provider and give them as much detail as possible about the error, including when it occurs and the steps you have taken to troubleshoot it.