What is a 502 Bad Gateway Error?

A 502 bad gateway error is triggered when your WordPress hosting server gets an invalid response for the requested page.
Bad gateway error is one of the common WordPress errors that you may encounter on your site. It can be caused by a number of reasons and depending on your server the error message may also differ in looks slightly.
The most common reason for a 502 bad gateway error is when your request takes too long for the server to respond. This delay can be a temporary glitch caused by high traffic. It could also be a glitch caused by a poorly coded WordPress theme or plugin. Last but not least, it can also happen due to a server misconfiguration.

Fixing the 502 Bad Gateway Error

This error is usually caused when your server is unable to find the cause of the invalid response. This means that we will try different troubleshooting steps until we find the problem.
Ready, let’s get started.

Step 1: Reload Your Website

Sometimes your server may take longer to respond due to increased traffic or low server resources. In that case, the problem may automatically disappear in a few minutes. You should try reloading the web page that you are viewing to see if this was the cause.

Step 2: Disable the CDN or Firewall

If you are using a CDN service or website firewall on your website, then it is possible that their servers are acting up. To check this, you will need to temporarily disable CDN.
Once disabled, it will remove the additional layer that has been created between your browser and the hosting server. Your website will now load entirely from your server and if the issue was caused by the CDN / firewall service, then it will get resolved now.
You can then contact your CDN service provider for support. Once the issue is resolved, you can go ahead and enable the CDN again.

Step 3: Update WordPress Themes and Plugins

If the error is still there, then the next step is to check your theme or plugins.
First, you will need to deactivate all WordPress plugins via FTP. After that, visit your website to see if the error has resolved.
If it is, then one of your plugins was causing the issue. You now need to activate all plugins one by one until you can reproduce the error. This will help you locate the plugin responsible for the error.

Step 4: fastcgi_buffers in Nginx server configuration

You can fix this error by adding fastcgi_buffers in nginx configuration. For this,

Open /etc/nginx/sites-enabled/domain.conf

Add below lines in the nginx configuration under line location PHP.

fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;

If it didn’t resolve also try adding these lines.

proxy_read_timeout 3600;
proxy_send_timeout 3600;

Step 5: PHP Timeout

When PHP reaches the allowed maximum time in server configuration to execute a script, it shows an error. It can sometimes trigger WordPress 502 Bad Gateway Error. To avoid this, simply increase the maximum execution time value. By default, it is set to 300 seconds, which is 5 minutes.

Conclusion

Now you have read this article and know why and how bad gateway error occurs in WordPress site, so you can troubleshoot them quickly. Whenever you see a “Bad gateway 502″ on your WordPress site, it is easy to sense that something went wrong with your website. Having the right knowledge about bad gateway error can save a lot of time troubleshooting. You don’t have to wait for someone else to fix your problem if you already know what the problem is.