HTTP 504 Gateway Timeout and HTTP 502 Bad Gateway errors are the most common server errors for WordPress website visitors and owners. In my previous article, we’ve already discussed the causes of a 504 Gateway Timeout error and possible solutions for it. In this article, we’ll come to understand what a 502 Bad Gateway error is, talk about the reasons for a 502 Bad Gateway error, and explore some tips for troubleshooting this type of errors.
What does 502 Bad Gateway mean?
Before we define what a 502 Bad Gateway error is, let’s deep-dive into server infrastructure and find out the meaning of some terms, like web server, proxy server or gateway, and upstream server.
Let’s first discuss how hosting a modern web application works. For this we need three actors:
- The web application
- The gateway
- The web server
In the picture below you can see the PHP modern web application workflow.
A web application is application software that can be coded in different programming languages and can use specific frameworks or libraries. It typically has tools to handle HTTP requests. For your WordPress website, the web application is your WordPress installation which is coded in PHP.
The gateway sits between a web server (Nginx, Apache) and a web application. It accepts requests from a web server and translates them for a web application. The exact definition of a gateway is somewhat fluid. Some call themselves process managers, some call themselves HTTP servers.
Here’s what the common functionality of a gateway entails:
- Listening for requests (HTTP, FastCGI, uWSGI, and more)
- Translating requests to application code
- Spawning multiple processes and/or threads of applications
- Monitoring spawned processes
- Loading balance requests between processes
PHP-FPM (PHP-FastCGI Process Manager) is the gateway for PHP. It is an implementation of FastCGI and will listen for FastCGI requests from a web server.
FastCGI is a binary protocol for interfacing interactive programs with a web server. CGI (Common Gateway Interface) is a web technology and protocol which describes a way for a web server to communicate with external applications, e.g. PHP. CGI is an interface between the web server and the dynamic web content that is generated by web applications that are written in different programming languages, such as PHP, Python, etc. FastCGI is an improved version of CGI.
A modern way to run PHP applications is to use PHP-FPM. Before PHP-FPM, PHP was commonly run directly in Apache, there was no need for a gateway. Apache’s PHP module loaded PHP directly, allowing PHP to be run in line with any processed files.
The web server generally hosts multiple sites, serves static files, proxies requests to other processes, performs load balancing and HTTP caching. The most popular web servers are Apache and Nginx. Apache used to be the most widespread web server until Nginx became more popular.
At 10Web we support LEMP stack, which is similar to LAMP (Linux, Apache, MySQL, and PHP), except Apache is replaced with Nginx. What happens when you open your WordPress website hosted by 10Web in your browser? The web server, in this case, Nginx, accepts a request and relays it to PHP-FPM, which in turn interprets the PHP code. The response is relayed back, finally reaching the client. In this case, Ngnix acts like a proxy server which in most cases is called an edge server. The server behind the proxy server is called upstream or origin server. In this case, PHP-FPM acts as an upstream server.
Now that we fully understood the above-mentioned terms, let’s finally understand what the 502 Bad Gateway error is.
What is a 502 Bad Gateway error?
We face a 502 Bad Gateway error when the web server acts as a proxy server and receives an invalid response from the upstream server. A 502 Bad Gateway error indicates that the proxy server, which is the edge server, was not able to get a valid response from the upstream server, which is the origin server. When you see a 502 Bad Gateway error it means that something is wrong with the upstream server. This can happen because of various reasons which we’ll cover in this article.
The different forms of 502 errors
A 502 Bad Gateway error can appear in different ways depending on the operating system, web browser, and device. Here’s how it looks most of the time:
Some websites customize 502 Bad Gateway pages. Here’s Google’s:
Platforms can also change the message of the error. So you can encounter different messages for the same error but they all have the same meaning:
- 502 Bad Gateway
- HTTP Error 502 Bad Gateway
- Error 502
- HTTP 502
- HTTP Error 502 – Bad Gateway
- 502 Proxy Error
- 502 Server Error: The server encountered a temporary error and could not complete your request.
- 502 Bad Gateway NGINX
- 502. That’s an error. The server encountered a temporary error and could not complete your request. Please try again in 30 seconds. That’s all we know.
What are the reasons behind the 502 Bad Gateway error?
The 5xx status codes indicate that there are problems with the server, and 502 is not an exception. For some reason, the proxy server can’t get a response or a valid response from the upstream server. In your WordPress website with Nginx/PHP-FPM stack, a 502 error can happen when PHP-FPM is not running or Nginx can’t communicate with PHP-FPM for some reason. This case should be checked by your hosting provider. Another reason could be PHP-FPM timeout issues, which we’ll discuss down the line.
Any misunderstanding between Nginx and PHP-FPM can lead to a 502 Bad Gateway error. Though these errors are connected to server-side issues, there are some tips for troubleshooting on the client side.
Let’s go over both client- and server-side troubleshooting.
How to Troubleshoot a 502 error message
Here are some very simple ways of fixing 502 Bad Gateway errors from the client side.
Reload the page
The first thing you should do is reload the page and wait for a minute. If the 502 Bad Gateway error disappears, it means there was a temporary problem with the upstream server or the networking between servers. If the error remains, check if the site is down for everyone. You can use Is it down right now? for this. If the site is up for everyone except you, open the site on another browser or in private mode.
Clear browser cache
Another easy tip is to clear the browser cache. If the error disappears after cleaning cache, it means that there was a temporary problem that has been resolved, but because of cache, you kept seeing the 502 Bad Gateway error template, instead of your website. If the error remains, try the next tip.
Flush DNS cache
The 502 Bad Gateway error can occur because of DNS issues. Operating systems, such as Linux, Windows, and macOS save name resolution information in the form of a DNS cache. In many cases clearing the DNS cache can solve a 502 Bad Gateway error. Here are the commands which you can use for flushing DNS cache on Windows, MacOs, and Linux.
Use this command to flush cache on Windows:
On macOS, you should open the terminal and type:
sudo killall -HUP mDNSResponder
There’s no message after processing this command, but you can add your own by running the command like this:
sudo killall -HUP mDNSResponder; dns cleared successfully
Things are different in Linux, as different Linux distributions use different DNS services. Some of them are NSCD (Name Service Caching Daemon), dnsmasq, and BIND (Berkeley Internet Name Domain). For an NSCD DNS cache:
sudo /etc/init.d/nscd restart
For a dnsmasq DNS cache:
sudo /etc/init.d/dnsmasq restart
For a BIND DNS cache:
sudo /etc/init.d/named restart sudo rndc restart sudo rndc exec
If the terminal asks for your password, just enter it.
Change DNS servers
You can also try to temporarily change your DNS servers. More information about changing DNS servers can be found in this article: Change your DNS servers settings.
If you’re using Cloudflare
Cloudflare returns a Cloudflare-branded HTTP 502 error when your origin web server responds with a standard HTTP 502 bad gateway:
This means that something is wrong with your origin server and you can try to use the above-described tips to fix the issue.
If the 502 error is from Cloudflare, the page looks like this:
If the error contains the word “Cloudflare,” the problem comes from Cloudflare, otherwise, it is from the origin server. In the first case, you can contact Cloudflare support, and in the second case, you can follow the described tips. If nothing helps, contact your hosting provider. You can read more about Cloudflare 5xx errors in the article Troubleshooting Cloudflare 5XX errors.
We’ve discussed some client-side tips which can help you troubleshoot a 502 Bad Gateway error. Now let’s see what you can do on the server-side.
The very first step is to restart your PHP. With 10Web, you can do this by going to Hosting Services > Tools and clicking the blue “Restart PHP” button.
If your hosting doesn’t provide an interface for restarting PHP, ask them to do it for you.
Checking your server error logs can give you very useful information about 502 Bad Gateway errors. With 10Web, you can easily check server logs by going to Hosting Services > Logs.
If you have access to your file system, you can check server logs. In the case of the Nginx web server, you can find logs here:
In the case of Apache web server, the logs are in this repository:
Improper firewall configuration
Improper firewall configuration can lead to 502 Bad Gateway errors. A firewall is a network security system that monitors and controls the incoming and outgoing network traffic based on predetermined security rules. It typically establishes a barrier between a trusted network and an untrusted network.
There can be cases that some awkward firewall settings can consider safe and valid content malicious and, consequently, cut off traffic which in turn cause 502 Bad Gateway errors. Check your firewall configuration to reveal any improper configs.
Third-party plugins & themes
Non-optimal codes in WordPress plugins and themes can also cause 502 errors. So, check your plugins and theme. If you have access to your WordPress admin, deactivate all your plugins, and if the error disappears it means that there is at least one guilty plugin. Then activate them one by one to find the guilty ones. If your WordPress admin area can’t be reached because of the error but you have access to your WordPress files, just rename the plugins directory in wp-content. It will deactivate all plugins. And again start activating them one by one.
If the problem isn’t the plugins, that is deactivating all plugins or renaming plugins directory doesn’t change anything, try to temporarily change your theme to WordPress’s default theme. Once you find the bad plugins or theme, connect to the respective support team and describe the issue.
And don’t forget to keep your plugins, theme, and WordPress core up-to-date. This will help you avoid many problems, including 502 errors.
Restart PHP-FPM service
You will get a 502 error if the PHP-FPM service is inactive or not running on your server. If you have access to your hosting, you can check this by running one of the following commands. For SysVinit:
sudo service php7.4-fpm status
sudo systemctl status php7.4-fpm
If the service is active and running, the output of the command should be like this:
If the status is not Active: active(running), try restarting PHP-FPM service to resolve the error using one of the following commands. For SysVinit:
sudo service php7.4-fpm restart
sudo systemctl restart php7.4-fpm
The 502 error can be caused by a PHP-FPM timeout. If your application is taking too long to respond, your users will experience a timeout error. If the PHP-FPM timeout is less than Nginx timeout, Nginx will return a 502 Bad Gateway error. To avoid this, you can increase PHP-FPM timeout if you have access to your server.
PHP-FPM timeout is set in pool configuration which is
The default value for this directive is 20 seconds. If you don’t have access to your server, ask your hosting provider to check it. To avoid getting 504 errors after increasing PHP-FPM timeout which can be because of Nginx timeout, the default is 60 seconds, you can increase fastcgi_read_timeout directive in /etc/nginx/nginx.conf file. Don’t forget to reload the Nginx server after changing the directive:
nginx -s reload
PHP execution time errors can also lead to 502 Bad Gateway errors. To avoid this, you can increase the PHP configs, such as max_exexution_time and max_input_time.
If you have your server access, just change these directives in your php.ini file. If not, ask your hosting provider to do it for you.
What is the difference between a 404 error and a 502?
A 404 Not Found error occurs when content can’t be found by the web server. A 502 Bad Gateway error happens when the proxy server can’t get any response or gets an invalid response from the upstream server. You come across 404 when requested content was removed or doesn’t exist. You see 502 errors when there is an issue with the upstream or origin server or a communication issue between proxy and upstream servers.
What is the difference between 502, 503, and 504 error messages on websites?
You got a 502 Bad Gateway error when the proxy server doesn’t get a valid response from the upstream or origin server. 504 Gateway Timeout error happens when the server which is acting as a proxy server can’t receive a timely response from the upstream server. 503 Service Unavailable error indicates that the server is not ready to handle the request, this happens when the server is down for maintenance or is overloaded.
Do 502 errors have any impact on website rankings?
The 502 Bad gateway error can have a major impact on website rankings. You don’t have to worry about a negative impact on SEO if the error lasts a few minutes. If the page is being crawled during this time, the crawler can load it from the cache. But you do need to worry if this error lasts for a few hours. In that case, Google will see the 502 error which can negatively impact your rankings.
What can I do when PHP is working in the command line but returns a 502 error in the browser?
502 error happens because of bad communication between proxy and upstream servers. When you’re running PHP in the command line you don’t need a web server, PHP works for you directly. To find out the reasons for 502 errors, read the above-described tips.
See How 10Web Can Benefit You
Visit our homepage to learn more about the first-ever AI-powered WordPress platform.
Now you have a complete understanding of what 502 errors are and why they appear on your website. We’ve discussed the various possible reasons for these errors and described many troubleshooting approaches that’ll help you find a solution.
That was all for now. Feel free to leave a comment and let us know if we managed to provide a suitable way for you to troubleshoot your bag gateway error!