I am getting 504 timeouts message from nginx when my PHP script is running longer than usual. set_time_limit(0) does not seem to prevent that! Does it not work
set_time_limit(0)
Since you're using php-fpm you should take advantage of fastcgi_finish_request() for processing requests you know can take longer.