Web developers often observe this kind of mistake on the servers with nginx as the frontend and Apache as the backend. The problem is usually assigned to nginx and the latter gets strenuously cured. Quite often it’s not the point, anyway.
It should be admitted that 504 Gateway Timeout happens when nginx sends client Apache requests but Apache fails to return HTTP-response within the established limit of time.
So the essence of this problem is that Apache failed to respond. It’s possible that the site workload has increased and Apache gets short of time to give HTTP-responses while the new requests keep gathering in line. Clients are stuck in the line. If they are not served in the established time, connection fails.
In order to solve the given problem, one should first of all try to add resources to the server, for example, RAM and to increase the number of HTTP processes in Apache.
It’s also worth trying, though it’s not that easy, to identify the pages and the modules that might have caused the overload and to optimize them so that they would work faster. It takes more time and a good deal of qualification to accomplish that job, though.
The hosting must be analyzed, too. In case we deal with an ordinary virtual hosting, it's hardly possible to solve this problem without Drupal having been tuned. Because as a rule there is no access to the hosting itself, you have what was given. Probably, the hosting will have to be changed. In any case, you can always rely on our Drupal website hosting.
It might also help if the time limit established for the script execution be increased, namely, the value of the PHP parameter max_execution_time.