We came across an issue recently on a server where all sites were throwing a 504 gateway error when a single WordPress site was under high PHP load.
This was a Plesk server but the same issue could happen on any server using Nginx with Apache, e.g. it could happen on a CloudWays server.
Initially we had thought the issue was in PHP with PHP workers being busy or some other issue with max children settings or similar.
After hours of troubleshooting and messing with dedicated and shared PHP pools, changing from PHPFPM to FastCGI and changing PHP versions the issue was still present so it appeared PHP was not the root cause.
NOTE: There are certainly cases where increasing the PHP max_children setting max fix this 504 error problem. On Plesk, changing to a dedicated PHP-FPM pool instead of shared pool maybe help – it wasn’t the fix in this case but it might be in yours.
Root Cause – Apache connection limit reached
It turns out this particular site was receiving hundreds of admin-ajax POST requests per minute tying up PHP and hitting the connection limit between Nginx and Apache. Once the Apache connection limit was reached Nginx started showing a 504 gateway error for all sites because Apache was no longer responding to it.
The solution in our case was to switch the one problematic site to use Nginx only without the Nginx+Apache configuration. This essentially eliminated the root cause of the problem because Nginx processed all requests and didn’t connect to Apache. NOTE we only made the change for this particular site, we left the rest of the sites as Nginx proxy+Apache.
We still had a problem with the one site which continued to throw 502 and 504 errors but this was due to a combination of a poorly built plugin and the user using that plugin incorrectly and generating the insane number of admin-ajax POST calls.