Hi there,
I would like to know when the 504 error occurs as in which use case of your website such error appears and how often does this happen?
Have you run detailed log analysis so far?
Whats your web server time out time configure correctly?
There are many reasons this might be happening and we need to find all loop holes and fix them one by one,
If you are running a vps i can install a control panel for you so you can manage the server efficiently
Let's talk!