The 502 Bad Gateway error indicates that a server has received an invalid response (or a response it can’t understand) from another server.

In this KB, we will discuss the four most common causes behind 502 error and what you can do in each situation. This KB will cover:

  • Server overload

  • PHP request timeout

  • Service failure

  • Plugin or theme conflict

Server Overload

When a server starts to get overloaded, its performance starts to degrade. As a result, the hosted websites start loading slowly, and eventually, 502 error starts to pop up.

Servers get overloaded due to several reasons including excessive CPU and memory usage. Cloudways offers an integrated platform-level server monitoring tool to simplify server resource monitoring. If you are familiar with Linux, you can monitor server resources using the command line tools such as the htop to overview all the threads and server processes in real-time.

Another cause of server overload is when too many website visitors cause too many connections to the database. As this number rises, the server overloads. You can monitor MySQL connections from the monitoring graph section.

You can also monitor MySQL resource utilization and active process through SSH commands such as htop, ps aux commands, and mytop command [mytop -u{dbuser-name} -p{db-password} dbname)] as master_userto check in real-time the MySQL connections that are executing queries. To deal with this issue, you can consider increasing the number of max MySQL connections to reduce database load.

Alternatively, if you see that all the traffic on the website is legit, it is time to scale server resources to tackle the increasing server visitors.

Servers can also get overloaded in case of attacks such as DoS/DDoS that are carried out from specific IP pools. To counter such as these, you can block an IP address from the .htaccess file using the following lines (please replace the IP addresses in the below command with the IPs that you need to block):

<Limit GET POST>
order allow,deny
deny from 46.228.47.114
deny from 46.225.88.5
allow from all
</Limit>

Frequently executed cron jobs and scheduled tasks consume a significant portion of server resources (in particular high CPU utilization). To counter this issue, try to space out cron job execution cycles by a minimum of 15 minutes.

PHP Request Timeout

Usually when PHP scripts take too long for execution and the allocated time limit expires, a Fatal Error: Maximum Execution Time Exceeded error is recorded in the Apache error log file.

To prevent this error, you need to increase the max_execution_time. For this, go to Server Settings & Packages and click the Basic tab. In the tab, increase the value for the execution time. Next, go to Application Settings and then the PHP-FPM Settings. Search for the line ;php_admin_value[max_execution_time]and increase the value of the parameter. If you get stuck anywhere in the process, check out this simple guide on PHP-FPM settings.

Service Failure

Our stack uses NGINX as a reverse proxy that depends on backend services like PHP-FPM and cache services to run web applications. If any of the services crashes or freezes, NGINX won’t get any data from them, resulting in a 502 Bad Gateway error. If this is the case, the solution is simple: restart Apache, NGINX, PHP-FPM, and purge Varnish from the Manage Services section.

Plugin or Theme Conflict

If you have tried the above three options, and the 502 error still persists, the problem is most likely caused by an installed theme or plugin.

Discovering the culprit could take time. The process involves disabling all the plugins and themes. Then the process starts by activating individual plugins and then testing the website in the browser. If the error persists, deactivate the active plugin, activate the next plugin and the cycle continues. The process is similar for themes. In many cases, disabling all themes and going back to the default theme solves the issue.

Once you have found the offending plugin/theme, contact the developers to bring the issue to their notice.

That’s it! We hope this article was helpful. If you need any help, then feel free to search your query on Cloudways Support Center or contact us via chat (Need a Hand > Send us a Message). Alternatively, you can also create a support ticket.

Did this answer your question?