If you're experiencing gateway timeouts (HTTP error 504) when using the Magento image upload/media manager or other CMS functionality, it may be because the server can't resolve its own domain name. Test this from the server's command line by trying to 'curl' a file via the site's externally-accessible domain name:
curl -o /dev/null http://www.domain.com/image.png
Magento uses the gd library to directly obtain image details from the URL of the image, rather than the local file system path, so if it can't connect the php-fpm gateway is likely to timeout (504 error).
Magento should log the errors in its var/log/system.log or var/log/exception.log files.
A useful way of debugging php-fpm gateway timeouts in general is to enable the php-fpm slow log in the pool configuration file (usually /etc/php5/fpm/pool.d/www.conf):
slowlog = /var/log/php-slow.log
request_slowlog_timeout = 30
If you then re-create the timeout and check the slow log file, it should give you a 'stack trace' showing the line of PHP source which caused the problem.