The 502 Bad Gateway error can occur in all browsers on all devices. It is a common error that can occur on any website. If you’ve ever come across a 502 Bad Gateway error, then you know just how frustrating it can be.
You now have a 502 Bad Gateway error. Your internet connection seems to be fine. Your computer looks fine. What are you seeing and how can it be fixed?
This common error is usually caused by a problem with the server or network, and it can be really tough to track down and fix. In this blog post, we’ll take a look at what causes 502 Bad Gateway errors, and how you can go about fixing them.
It can seem like you are solving a mystery when your website experiences a 502 Bad Gateway error. It’s hard to know the exact cause of your website’s 502 Bad Gateway Error. All you can know is that there’s something wrong and you have to fix it.
Table of Contents
- 1 What is a 502 Bad Gateway Error?
- 2 502 Errors Have an Impact on SEO
- 3 What causes a 502 Bad Gateway Error?
- 4 What does 502 Bad Gateway mean?
- 5 How to Fix a 502 Bad Gateway Error
- 5.1 Reload the page
- 5.2 In Incognito Mode
- 5.3 Check for issues with server connectivity
- 5.4 Verify for DNS changes
- 5.5 Check your server logs
- 5.6 Temporarily disable CDN and Firewall
- 5.7 Check the code bugs
- 5.8 PHP Timeout Issue
- 5.9 Get in touch with your host
- 5.10 Restart PHP
- 5.11 Fix 502 errors in the NGINX/PHP-FPM stack
- 6 Final lines
What is a 502 Bad Gateway Error?
A 502 Bad Gateway error is a general indication that something is wrong with a website’s server communication. It is a generic error and does not indicate the exact problem. This will cause your website to display an error page to visitors.
The 502 bad gateway error can happen on any server, be it Apache, Nginx, or Tom Cat. There are several effective and common solutions for most 502 Bad Gateway errors.
This problem may require solutions specific to WordPress, Joomla, or other open-source code. However, in most cases, the server is the problem to find out first.
502 Bad Gateway Error Variations
A 502 bad gateway error could appear in many different ways due to the variety of browsers, web servers, and operating systems. They all have the same meaning.
Here are just a few of the many variations that you may see on the internet:
- 502 Bad Gateway
- Error 502
- HTTP Error 502 – Bad Gateway
- 502 Service Temporarily overloaded
- 502 Proxy Error
- Blank white screen
- 502 Server Error – The server encountered a temporary problem and was unable to complete your request
- HTTP 502
- Temporary Error (502)
- 502. This is a mistake
- Cloudflare 502 Bad Gateway
- Bad Gateway: An invalid response was received from the upstream server by the proxy server
In case the 502 error has been configured to display a message board or a separate page, server-side checks may be needed to better understand the error and its cause.
Here’s another variation:
“502: Web server received an invalid response while acting as a proxy or gateway server. The page you’re trying to display is not available because there’s a problem. The Web server was acting as a proxy or gateway to the upstream content servers and received an inexplicable response.”
For domains using Cloudflare, a 502 Bad Gateway means that Cloudflare’s network cannot connect to your server for communication. Cloudflare 502 Bad Gateway Error is an HTTP status code that indicates that the server on Cloudflare received an invalid response from the server containing the website’s source code.
Twitter, for example, might display a different message regarding a 502 Bad Gateway error. This message could be something like “Twitter over capacity.”
Google also sometimes gets the 502 error, and this is how Google shows this error message: “502. It is an error. Your request was not completed because the server encountered a temporary error. Please try again within 30 seconds. This is all we know.”
502 Errors Have an Impact on SEO
A 503 error is used to maintain WordPress and tell Google to come back later, but a 502 error could have a detrimental effect on SEO if it is not corrected immediately.
Even if your site is down for a few minutes, and the crawler is crawling it regularly, the crawler will still deliver the page from the cache. Google may not have the chance to crawl it again before it is back up.
Google may view the 502 error as an issue at the site level and report it to them if the site goes down for a prolonged period of time (e.g., 6+ hours). This could affect your rankings. You should investigate the reasons for repeat 502 errors if you are concerned about them.
What causes a 502 Bad Gateway Error?
Before taking you through how to fix the 502 Bad Gateway Error, you must first identify the possible causes of a 502 Bad Gateway error. Here are the most common causes of 502 errors.
Domain name not resolved
This occurs when a domain is not resolving correctly to an IP. It is possible for DNS changes to take up to 24 hours to propagate if your domain name has been moved to another host.
Overloaded origin server
When the origin server runs out of resources, it can crash and become overloaded, resulting in an HTTP 502 error. Server overload can be caused by traffic spikes, low memory, and software timeouts. Hosting server-side limitations like timeout, RAM, and CPU can also be the cause of 502 errors.
Browser errors
Sometimes, a browser may show a 502 error even though there are no network or server issues. This error could be caused by browser extensions that are not compatible with your browser, such as ad blockers or outdated browser versions.
Problems with home-network equipment
HTTP 502 error will only occur on a limited number of websites and very rarely. If you encounter this error on multiple sites and/or browsers, you should immediately think of an error in your intranet. This may indicate that your network device is experiencing temporary problems.
Blocks for the Firewall
Your website is protected from suspicious traffic by a firewall. Some firewall systems can detect false positives because they are too sensitive. It could be blocking a particular ISP or a request to a CDN.
What does 502 Bad Gateway mean?
When you attempt to open a webpage, you are sending a request for that page to be accessed to a web server. The web server processes your request and returns the requested resource along with an HTTP header code and status code.
There are many 5xx status codes available, including 500, 501, 502, 503, 504, 505, 506, 507, 508, and more.
These status codes of 5xx indicate that the server is having problems. These codes are not the fault of the client. This means that the request you made was valid, but the server could not generate the requested resource.
The 502 Bad Gateway error indicates that a server on the internet received an inexplicable response from another server. This error usually occurs when there is an interference from the proxy server
How to Fix a 502 Bad Gateway Error
A 502 bad gateway error is a fairly common HTTP status code that means the server (the web server) was acting as a gateway or proxy to the outside world, but something happened that prevented it from fulfilling the request.
This could be anything from something as simple as the server being down for maintenance to the upstream proxy itself being down or misconfigured.
The 502 Bad Gateway error is a very common error in web servers. Usually, when a web server returns this error, it means that the upstream server that is sending the web server the data has sent corrupted data.
The 502 bad gateway error also is one of the most common WordPress errors. If your website is experiencing 502 Bad Gateway errors and you are not sure how to fix it, here’s a quick guide to help you out.
Reload the page
Server connectivity problems can sometimes be resolved quickly. Be sure to take the necessary steps before you dig into the root cause of the problem.
Give it a few seconds. Reload the page. This could be a temporary problem with the connection. Clear your browser cache if the page still gives you an error. Next, refresh it. If this doesn’t work, you can continue to investigate the problem.
In Incognito Mode
It is also a good idea to access the website in Incognito mode. This helps you determine if the HTTP error 502 message is due to a browser-related problem. Click on the three dots button on your browser screen to open New Incognito Window.
You can open incognito mode by pressing Ctrl+Shift+N keys
A browser extension might be responsible if the error does not occur in Incognito mode.
Keep this in mind as you disable add-ons and extensions slowly. After you have deleted the error-causing add-on, you will be able to access the website.
Try switching to a different browser if disabling extensions is not an option. Mozilla Firefox is an example of a browser you might try.
Your previous browser could be the reason the page does not load normally in the new browser. You can fix the problem by uninstalling and reinstalling your browser.
Check for issues with server connectivity
Many websites are hosted on multiple servers or third-party hosts. Your website may display a 502 Bad Gateway Error page if your server goes down due to maintenance or other reasons. This is the only way to fix this error.
You don’t need to contact your hosting provider if you don’t want to. A ping test can be used to quickly determine if your IP is receiving messages. You can use tools available on your computers like CMD or Terminal to ping, or you can use this online ping tool: https://check-host.net/check-ping?host=huyhoa.net
Verify for DNS changes
It will make changes to the DNS server of your website if you have recently moved it to another IP address or changed its host servers. Your website could be serving visitors with a 502 Bad Gateway Error Page.
These DNS changes will take effect on the day your website is up and running. This can take several hours. You can check the status of your DNS on many servers in many locations around the world at this website.
Check your server logs
Server logs provide information about the health and status of your server. To find and respond to alarming information, you can sort through them.
If you get a 502 Bad Gateway error following a change or update, it could be an issue with your server. This issue can be fixed by checking your error log.
Your hosting service usually gives you access to enable website errors to log through an administrator dashboard. The following lines can be added to wp-config.php to enable error logging for your WordPress site:
define( 'WP_DEBUG', true ); define( 'WP_DEBUG_LOG', true ); define( 'WP_DEBUG_DISPLAY', false );
All of the generated errors will appear in the wp-contents/debug.log file, allowing you to detect which ones are causing a 502 Bad Gateway error.
Temporarily disable CDN and Firewall
Your content delivery network (CDN), or firewall could be the problem. You can temporarily disable your CDN if you’re using a third-party CDN provider.
We love the CDN Enabler plugin, which is free. You can deactivate it and then test your site. Is WP-Admin not working?
Log in to your site using SFTP to rename the folder of the plugin to CDN-enabler_old. This will temporarily disable your CDN connection. This is also true for WP Rocket and any other plugins you may have connected to your CDN.
Providers that offer DDoS mitigation and protection or fully proxy services such as Cloudflare can sometimes experience this issue. They have additional firewalls.
This is possible with all Cloudflare service plans. Cloudflare is a proxy service and cannot be quickly disabled.
For domains that use Cloudflare’s proxy, but 502 errors occur, there are 2 situations. The first is the 502 error caused by the Cloudflare server itself. In this case, you should contact them for assistance. You can check Cloudflare’s services page at their status page.
The second case is a 502 error that occurs on your web server itself. This can have many causes like php error, firewall misconfiguration. Your firewall protects your website from malicious visitors and distributed denial of service (DDoS) attacks.
Sometimes, bad firewall configuration can cause your firewall to reject requests from the content delivery network or the Cloudflare server itself. This can lead to a 502 Bad Gateway error. To fix the problem, check your firewall configuration.
Check the code bugs
Your server may not be able to respond to content delivery network requests if there’s a problem with your site’s code. To find bugs in your code, you may have to move it to a certain dev site and enable dev mode as well as debug mode to find bugs.
It will perform a detailed debugging process to simulate a 502 Bad Gateway Error situation and tell you exactly what happened.
Sometimes, invalid plugins and themes can be the cause of the 502 Bad Gateway error. Plugins or themes that are too old not to be updated, PHP that is too old or too new to be compatible with current code can also cause 502 errors.
Because poorly coded plugins and themes can cause conflicts with each other. This will cause your server to stop all scripts and queries on your site, making it inaccessible.
Some websites have issues with security plugins or caching. These plugins can block communication between servers and cause the error to persist.
For sites that use WordPress source code, if you don’t have the security or caching plugin installed and still get the HTTP 502 error, it’s best to disable all plugins and then re-enable them one by one. If you can still log in to the admin page, go to Plugins, then click Deactivate in the Bulk Actions menu.
Next, activate each plugin and refresh your WordPress site after each plugin. That way, you can pinpoint which plugin is causing the 502 error and will find a way to fix it.
If you cannot access your WordPress dashboard, you will have to disable all plugins by renaming the plugins folder to disable all plugins. Login to your hosting control panel, navigate to Files Manager, or use an FTP program to log in to the file manager on your hosting. Navigate to the wp-content
folder and rename the plugins
folder, such as plugins-disable
.
Cpanel hosting users can access their wp-content
by logging into Cpanel and clicking on File Manager. Go to File Manager->public_html->wp-content.
Once your site is back up after you have disabled all plugins, you can rename the plugins-disable folder to plugins. Next, enable plugins one by one to determine which plugin is causing the problem.
Developers and website owners need to be familiar with error codes to quickly identify the problem and fix it. Your site may experience status errors other than HTTP 502 Errors.
These are the most common errors for a website other than the 502 error:
- 403 Forbidden Error. This occurs when a web server blocks visitors from accessing certain content.
- 404 Page not found. Refers to a response that is sent when the requested page is unavailable.
- 501 Not Implemented. This means that the server does not have the capability to fulfill a request.
- 503 Service unavailable. This indicates that the server is down for maintenance and does not process requests.
- 504 Gateway Timeout. The proxy error occurs when one waits too long to receive a response from another server.
PHP Timeout Issue
PHP timeout is caused by PHP processes running more than the maximum execution time and maximum input time specified in your PHP configuration.
This usually happens when the server has to process large or large amounts of data, reducing the server response time. This is when the 502 error occurs. These values may need to be increased if you experience PHP timeouts.
Check with your current host provider to see the parameters of your server settings. If they are adjustable, turn them up as much as possible. For example, the maximum execution time is usually set to 30 seconds by default on most hosting platforms.
The maximum PHP timeout value on the platform is also usually set at 30 seconds. If necessary, you might consider increasing these values to 60 or 120 seconds.
Read more about how to solve the PHP timing out problem in fix 502 bad gateway errors on the Nginx server – PHP-FPM below
Get in touch with your host
You can call your hosting provider if you are unable to resolve the issue or you suspect the error is caused by a server-side error that you cannot intervene. They can help you understand the problem better.
They can fix the problem if it’s their fault. They can help you if the problem is specific to your site. In some cases, your hosting provider’s technical support staff can even fix your site’s code if it’s the main problem causing your website’s errors.
Restart PHP
You can also restart PHP if you have a VPS or Dedicated Server.
With most shared hosting, you cannot restart PHP yourself, you will need to contact your hosting provider. Particularly with Dreamhost, you can manually restart the PHP process, even with shared hosting packages.
Dreamhost offers many ways to kill all PHP processes and restart PHP for shared hosting. You can also open support tickets to request that PHP is restarted if your hosting does not offer it.
Fix 502 errors in the NGINX/PHP-FPM stack
The NGINX service will return a 502 Bad Gateway error if it cannot successfully receive a response from the PHP-FPM service or if PHP-FPM is unresponsive.
Some possible causes of 502s on Nginx – PHP-FPM:
- PHP-FPM not running
- NGINX cannot communicate with PHP-FPM
- PHP-FPM timed out
- Wrong declaration of PHP-FPM’s run method (socket and listening port). For example, Nginx claims to run on port 9000 (listen = 127.0.0.1:9000) but php-fpm claims to run on unix socket (listen = /run/php/php8.1-fpm.sock)
If the NGINX service is unable to communicate with PHP-FPM for any of these reasons, it will respond with a 502 error. This error is easily viewable in Nginx’s access log (usually located in the Nginx access log). /var/log/nginx/access.log)
PHP-FPM not running
PHP, not running can be due to many reasons. It is possible that the server is restarted but the php-fpm service is not declared to run when the machine is restarted. You can enable it with the following command:
sudo systemctl enable php-fpm
Note to change php-fpm to the correct service on your server. if you have changed the service or you have multiple versions of PHP installed – for example, the following command will enable the php-fpm 8.1 service:
sudo systemctl enable php8.1-fpm.service
After enabling, start the php-fpm service with the command
sudo systemctl start php-fpm
or
systemctl restart php-fpm
NGINX can’t access the socket
When php-fpm starts, it creates one or more TCP/Unix sockets to communicate with the Nginx web server.
Both these services must declare the same communication method in order for them to communicate with each other. That is, if php-fpm declares to communicate via Port, Nginx must also declare php-fpm to run through Port, and if php-fpm declares to run through Unix sockets, Nginx must also declare the same.
We can easily find the declarative configuration of php-fpm in the configuration file of php-fpm.
To find the php-fpm file, we need to know what version of php-fpm is running, for example, 7.4 or 8.1, then we will search with the locate command:
locate php-fpm.conf
Or
locate php74-fpm.conf
Or
locate php81-fpm.conf
Or
locate www.conf
Once you have found the php-fpm configuration file, you can find the line that declares its runtime port, possibly like this:
listen = 127.0.0.1:9000
Or
listen = /run/php/php8.1-fpm.sock #Ubuntu/Debian
Or
listen = /run/php-fpm/www.sock #CentOS/RHEL/Fedora
Next, find the Nginx configuration file, it will usually be in a location something like this: /etc/nginx/conf.d/huyhoa.net.conf
(replace huyhoa.net.conf
with your domain.conf
)
Look in the Nginx configuration file to the PHP location declaration, there will be a paragraph like this:
fastcgi_pass unix:/run/php/php7.4-fpm.sock #Ubuntu/Debian
OR
fastcgi_pass unix:/run/php-fpm/www.sock #CentOS/RHEL/Fedora
Change the configuration of this declaration so that it is the same as the one in the configuration file of php-fpm.
For Centos 8, PHP-FPM runs as an upstream so it should look like this:
upstream php-fpm { unix server:/run/php-fpm/www.sock; }
PHP-FPM is timing out
request_terminate_timeout
value of PHP-FPM is set to 20 seconds by default, while Nginx’s is set to 60 by default, so sometimes it can cause php-fpm to time out and not return results to the Nginx web server. Nginx will return a 502 bad gateway error to the client browser.
If you see in the error.log error log errors like this then the exact 502 error is caused by the php-fpm timeout error:
2022/08/06 18:28:58 [error] 3059#3059: *29 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 127.0.0.1, server: huyhoa.net, request: "GET/HTTP/1.1", upstream: "fastcgi://unix:/run/php/php-fpm.sock:", host: "huyhoa.net"
The best way to handle the php-fpm timing out error is to increase the time out for Nginx because if you increase the time out for php-fpm, it is very likely to get a 504 Gateway Timeout error because Nginx closes the connection before getting a response signal from php-fpm. We open the Nginx configuration file at this location: /etc/nginx/nginx.conf
Find the http block and change the following three values: fastcgi_connect_timeout, fastcgi_send_timeout, fastcgi_read_timeout
. Here is an example when increasing the timeout to 120 seconds
http { ... fastcgi_buffers 8 32k; fastcgi_buffer_size 64k; fastcgi_connect_timeout 120; fastcgi_send_timeout 120; fastcgi_read_timeout 120; }
After editing the configuration values of php-fpm and Nginx, we must first try to see if there are any errors with the nginx -t
command. If you get a message like this then you can relaunch both the php-fpm and nginx services for the changes to take effect:
sudo systemctl restart nginx sudo systemctl restart php-fpm
Or without sudo
if you are running as root
systemctl restart nginx systemctl restart php-fpm
Final lines
There are many things you can do to fix the 502 Gateway error. This is usually not an issue on the client-side but a problem with your server. You should also be aware of third-party plugins, themes with unoptimized queries, or poorly written code.
If the gateway server receives an incorrect response from its origin server, then the 502 Gateway error is a problem. This error can affect conversion rates, user experience, page rank, and other factors if it is not addressed.
The server side is often the source of the problem, as indicated by the status code. The problem is not with the website, your internet connection, or your computer. It is important to watch out for broken plugins and themes.
Hopefully, the above troubleshooting solutions will help you get rid of any errors and get your site back to normal. We’d love to hear your suggestions and solutions. Maybe you have some tips to better fix the 502 Gateway error that we haven’t covered in the article.
Frequently Asked Questions
What Is a 502 Bad Gateway Error?
The HTTP 502 Bad Gateway server error response code indicates that the server while acting as a proxy, received an invalid response from the upstream server. Bad Gateway errors are often caused by problems between online servers that you normally have no control over.
502 Bad Gateway Error in Cloudflare: For domains that use Cloudflare DNS and have a proxy enabled, a 502 error can occur. Error 502 for domains that use Cloudflare means that the proxy server in Cloudflare received no response. valid from the origin server containing the site code.
Another variation you may see is "502 server error". And here is another variation. “502 - The web server received an invalid response while acting as a gateway or proxy server.”
What Causes the 502 Bad Gateway Error?
Error 502 bad gateway is a temporary error that can occur for various reasons. Some common causes of 502 errors include:
- Due to a spike in traffic (Server overload)
- When the server's firewall blocks the client's request
- When the domain address cannot be resolved
- When the timeout has expired and there is still no response from the server
- Some browser extensions can also sometimes lead to errors, mainly the AdBlock extension.
How Do You Fix the 502 Bad Gateway Error?
There are a number of things you can try when attempting to fix the 502 Bad Gateway Error:
- Try Reloading the Page
- Clear Your Browser Cache
- Flush your local DNS cache
- Check With Your Host
- Temporarily Disable CDN or Firewall
- Check Your Plugins and Themes
- Check Your Logs
- Restart PHP
- PHP Timeout Issue