How to Fix 502 Bad Gateway Error?
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:
There’s no message after processing this command, but you can add your own by running the command like this:
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:
For a dnsmasq DNS cache:
For a BIND DNS cache:
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.
Restart PHP
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.
Check logs
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:
For SystemD:
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:
For SystemD:
Timeout issues
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:
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.
Comments
Post a Comment