Wordpress had this advice for webmasters whose sites were giving 504s. Repeat, this is for devs, not for us users whose fault it isn't. Fixing 504 Errors on Your Own Site
A lot of times this isn't your fault at all, but it's not
the user's either. Start by checking that your server can properly
resolve all the domains that your applications require access to.
Very heavy traffic might result in your server serving a 504 error, even though a 503 would probably be a bit more accurate.
In WordPress specifically, 504: Gateway Timeout messages are sometimes due to corrupted databases. Install WP-DBManager and then try the "Repair DB" feature, followed by "Optimize DB," and see if that helps.
Also, make sure your HTACCESS file is correct, especially if you have just reinstalled WordPress.
Finally, consider contacting your hosting company. It's possible that
the 504 error that your website is returning is due to an issue on
their end that they'll need to resolve.