This one had me scratching my head for a while – using php7.0-fpm and the latest nginx on Debian 8 Jessie I was seeing our PHP contact form working correctly but returning a Gateway 504 error upon submission, which was resulting in a great deal of resubmitted forms and error notifications from users.
This only started occuring after we moved our site to Digital Ocean – it turns out that it was a IP priority issue and the following resolved it:
and uncommenting the following line:
precedence ::ffff:0:0/96 100
Then a reboot. Voila, the contact form worked properly again.