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:

ipconfig /flushdns

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:
502 bad gateway appearance
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:
bad gateway error cloudfare
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.
10Web restarting php bad gateway error
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.
10Web logs hosting services
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:

/var/log/nginx

In the case of Apache web server, the logs are in this repository:

/var/log/apache2

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

For SystemD:

sudo systemctl status php7.4-fpm

If the service is active and running, the output of the command should be like this:
fixing 502 bad gateway error server-side
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

For SystemD:

sudo systemctl restart php7.4-fpm

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

request_terminate_timeout

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.

Comments

Popular posts from this blog

503 Service Unavailable Error: What It Is and How to Fix It

How To Fix Syntax Error?

Embed google form in wordpress