Increase PHP script execution time with Nginx

If you have a large WordPress setup or a server with limited resources, then you will often see the “504 Gateway Time-out” error.

You can follow the steps given below to increase the timeout value. PHP default is 30s.

Changes in php.ini

If you want to change max execution time limit for php scripts from 30 seconds (default) to 300 seconds.

vim /etc/php5/fpm/php.ini

Set…

max_execution_time = 300

In Apache, applications running PHP as a module above would have suffice. But in our case we need to make this change at 2 more places.

Changes in PHP-FPM

This is only needed if you have already un-commented request_terminate_timeout parameter before. It is commented by default, and takes value of max_execution_time found in php.ini

Edit…

vim /etc/php5/fpm/pool.d/www.conf

Set…

request_terminate_timeout = 300

Changes in Nginx Config

To increase the time limit for example.com by

vim /etc/nginx/sites-available/example.com
location ~ \.php$ {
	include /etc/nginx/fastcgi_params;
        fastcgi_pass  unix:/var/run/php5-fpm.sock;
	fastcgi_read_timeout 300; 
}

If you want to increase time-limit for all-sites on your server, you can edit main nginx.conf file:

vim /etc/nginx/nginx.conf

Add following in http{..} section

http {
	#...
        fastcgi_read_timeout 300; 
	#...
}

Reload PHP-FPM & Nginx

Don’t forget to do this so that changes you have made will come into effect:

service php5-fpm reload
service nginx reload

More

21 thoughts on “Increase PHP script execution time with Nginx

  1. Have you see this where you receive a HTTP 499 response from NginX rather than a 504? I’m having that problem with an AWS instance where I have a PHP script which terminates after 60 seconds. The script is

    I’ve increased the PHP execution time to 360 seconds as well as the fast_cgi_timeout but it still fails after 60 seconds.

    The script works from the command line though

    1. If you look into the php.ini file for “max_execution_time” you’ll see this note:

      Note: This directive is hardcoded to 0 for the CLI SAPI

      Therefore your script will never run out of time when you call it on the command line.

  2. Hi,

    We have been receiving this 504 Gateway Timeout using Nginx the last few days on our site.

    We have php handler DSO (as opposed to Fast CGI) running on our server. Does the above configuration tweak apply to server running DSO as php handler?

    Please advise.

    1. I am having issue when I do simple form submission using post action. I am getting 504 Gateway time out error
      Max_execution_time is set to 120 and we are using fast-cgi
      $_POST array is blank. Can you please help me in resolving this issue

      1. As far as I know, if the execution time on nginx-sides runs out, the php script will still be running in the background, but the user will get the “504 Gateway timeout”-error.

        On the other side, hitting the max-execution time for php-fpm or in php itself will kill the process, write something into the PHP log file (like “PHP Fatal error: Maximum execution time of XX seconds exceeded in”…). I don’t know what the fpm would write into the log, but it (for sure) will kill the php process.

        Just as a hint: I had some thoughts what would happen if you are uploading files … should the time for nginx include the time the user needs to upload data? If php is running as FastCGI process (as it is here), the answer is NO.
        See: http://stackoverflow.com/questions/16064282/nginx-when-is-the-fastcgi-proxy-triggered-after-request-header-or-after-reques/16187165#16187165

        1. Error message could be different but whoever dies first will break the chain.

          About uploads, normal HTTP file-uploaded are buffered at nginx-level. Sending them from nginx to PHP happens quite fast if PHP and Nginx are on same machine.

Comments are closed on this article.
Please use our free support forum for support and discussions.