Recently I had to allow a PHP script to run for several minutes under Nginx (more specifically, as part of a Laravel app on a DigitalOcean server), after encountering the error “504 Gateway Timeout”. First, to tell Nginx to allow the script to run for a longer period of time, I set the FastCGI read timeout to ten minutes in /etc/nginx/sites-available/default:
1 2 3 4 5 6 7 |
server { # … other directives location ~ \.php$ { # … other configuration fastcgi_read_timeout 600; } } |
Second, to increase the amount of time that this particular PHP routine could spend running, I increased the execution time limit programmatically before each processing block:
1 2 3 4 5 6 7 8 9 |
public function long_running_process ($blocks) { foreach ($blocks as $block) { // Add to the total execution time for this script. set_time_limit(120); // Perform another block of data processing. $this->process_block($block); } } |
With this configuration, other PHP scripts are still capped at the default timeout value set in php.ini, and no individual block of processing is allowed to run away with the full ten-minute time limit.