Nginx + php-fpm random hang
To me it looks like a problem with the FPM process management. See this link for more information. It's seems like you have static number of processes available for php-fpm and traffic is exceeding it. I was facing similar issues, but I don't remember if this was the exact error message back then. But the behavior was like you explained, some connections were really slow (Waiting for fpm?) other were going to 504.
UPDATE:To confirm my theory you should check the file /etc/php5.6/pool.d/www.conf
and check this line:
pm.max_children = X
Increase the number and restart fpm. Then see if problem is solved.
Try to modify your config as follows :
pm = ondemandpm.max_children = 200pm.process_idle_timeout = 1spm.max_requests = 1000
restart fpm and your problem should be solved.