-sandro- Posted May 22, 2012 Share Posted May 22, 2012 how to handle timeouts with PHP in php5-fpm + ngnix configurations? I tried to make a simple script with just sleep(60); php.ini max_execution_time = 30 fast_cgi fastcgi_connect_timeout 60; fastcgi_send_timeout 50; fastcgi_read_timeout 50; The script stops at 50s for timeout of the backend. What do I have to do to [*] enable the max_execution_time in php.ini [*]enable ini_set to change the execution time to 0 directly in the script Why does fast_cgi get to control timeouts over everything instead of php itself? Quote Link to comment https://forums.phpfreaks.com/topic/262919-php5-fpm-nginx-control-timeouts/ Share on other sites More sharing options...
kicken Posted May 22, 2012 Share Posted May 22, 2012 I tried to make a simple script with just sleep(60); php.ini max_execution_time = 30 time spent in sleep() (and other outside-of-php things, eg db calls, exec() calls, etc) does not count toward the max_execution_time. That directive is a limit only on the time PHP itself is actually doing something. To test that directive you just need to create a busy loop of some sort. Something like this should do: <?php while (true){ sha1(time()); }; It will eat up your process for a while but should be killed once the max execution time is reached. Quote Link to comment https://forums.phpfreaks.com/topic/262919-php5-fpm-nginx-control-timeouts/#findComment-1347730 Share on other sites More sharing options...
-sandro- Posted May 22, 2012 Author Share Posted May 22, 2012 kicken a big THANK YOU. The reason why things didn't add up for me is because on Windows things are completely different. In the documentation I found: The set_time_limit() function and the configuration directive max_execution_time only affect the execution time of the script itself. Any time spent on activity that happens outside the execution of the script such as system calls using system(), stream operations, database queries, etc. is not included when determining the maximum time that the script has been running. This is not true on Windows where the measured time is real. This explains why with apache and mod_php setting the timeout to 0 I could run scripts non-stop! So I suppose there's no way to overwrite the limit for fast_cgi directly in the script right? The only way is to launch scripts in CLI? Quote Link to comment https://forums.phpfreaks.com/topic/262919-php5-fpm-nginx-control-timeouts/#findComment-1347745 Share on other sites More sharing options...
kicken Posted May 22, 2012 Share Posted May 22, 2012 So I suppose there's no way to overwrite the limit for fast_cgi directly in the script right? The only way is to launch scripts in CLI? Correct. Long running scripts should not be run from a web-server anyway as it will tie up server resources for the duration of the script which can cause your server to be less able to serve normal requests. Quote Link to comment https://forums.phpfreaks.com/topic/262919-php5-fpm-nginx-control-timeouts/#findComment-1347749 Share on other sites More sharing options...
-sandro- Posted May 22, 2012 Author Share Posted May 22, 2012 Perfect. Very helpful! Quote Link to comment https://forums.phpfreaks.com/topic/262919-php5-fpm-nginx-control-timeouts/#findComment-1347751 Share on other sites More sharing options...
-sandro- Posted May 23, 2012 Author Share Posted May 23, 2012 I'm a little confused about fast_cgi though if the script duration is controlled in php.ini what do these exactly control? fastcgi_send_timeout 50; fastcgi_read_timeout 50; Quote Link to comment https://forums.phpfreaks.com/topic/262919-php5-fpm-nginx-control-timeouts/#findComment-1347831 Share on other sites More sharing options...
kicken Posted May 23, 2012 Share Posted May 23, 2012 fastcgi has it's own set of timeouts and checks to prevent it from stalling out on a locked up process. They would kick in if you for instance set php's execuction time limit to 0 (unlimited) then accidentally created an infinite loop. Or if you were running some other application besides PHP which didn't have any of it's own timeout protections and it failed. Quote Link to comment https://forums.phpfreaks.com/topic/262919-php5-fpm-nginx-control-timeouts/#findComment-1347843 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.