Jump to content

php5-fpm + nginx control timeouts


Recommended Posts

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?

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.