PHP actively disconnects ConnectionClose and ignore_user_abort from Nginx to run in the background. PHP actively disconnects ConnectionClose and ignore_user_abort in Nginx. the backend runs a program that calls SVN to synchronize updates from multiple servers in the past two days, to prevent com PHP from actively disconnecting from Connection Close and ignore_user_abort in Nginx
In the past two days, I got a PHP program that calls SVN to synchronize updates on multiple servers. to avoid being blocked during the commit process, I had to find a way to trigger requests only for half a day, it takes too long to wait for the program to complete the update and return results. Therefore, I have studied how to enable PHP to actively disconnect the connection. After one afternoon, I found a lot of problems. Fortunately, I finally got it, mainly because Nginx is too pitfall ..
The following code is used:
/*** Actively disconnect from the client browser * If the Nginx server needs to output data greater than or equal to fastcgi_buffer_size to be cached, the headers can be output instantly and disconnected, if it still does not work, you can try to close gzip * for example: fastcgi_buffer_size 64 k; that is, 64*1024 characters (more is not required) are required. * str_repeat ('', 65536) can be used ); in addition, str_repeat ('', 6554); this method is actually slower to generate * @ param null | string $ str current output content, if no output is required, set it to null */public function connectionClose ($ str = null) {$ str = ob_get_contents (). $ str; // if the actual output Content Length is less than this value, it may lead to an active disconnection failure header ('content-Length :'. strlen ($ str); Header: connectionClose (); ob_start (); echo $ str; ob_flush (); flush ();}
Additional instructions:
There was no problem with apache in general. I didn't find any problems during xampp debugging on windows at the beginning. as a result, Nginx was used on the server, and it was useless. it crashed for one afternoon, later, it was reported that it was the fastcgi_buffer problem of Nginx.
I tested N times in various cases, so there should be no bugs...
Besides, let's talk about the ignore_user_abort () function.
When the browser is closed, it determines whether the program will continue execution in the background. (in the example, you do not have to set limit 0 to never time out during the test, set it to one or two minutes. Otherwise, it may take a long time to restart the HTTP service)
To put it simply, if you want to close your browser and execute the program again, you must add the following code:
ignore_user_abort(true);
However, depending on the program (mainly the while endless loop) behind you, it is somewhat different:
Generally, you can monitor the connection status in the program for control:
$isAborted = connection_aborted();$status = connection_status();if (0 !== $status || $isAborted) { break;}
However, to make these two functions work properly, you must have output content in your program that is greater than the output cache of the current WebServer.
If you simply output a Space echo '', you may have to loop for thousands of times before judging, therefore, to detect the status more instantly, you must output enough content each cycle to trigger the status detection.
Therefore, we often encounter a problem: when the browser is disconnected, even if the ignore_user_abort (true) is not used, the program continues to run because there is no output, the endless loop will keep running. if it is set to time-out, it will be okay, otherwise it will actually die.
The following is the test code (the picture is mainly used for anti-theft ~)
set_time_limit(0);ignore_user_abort(true);while (1) { echo str_repeat(' ', 65536); $isAborted = connection_aborted(); $status = connection_status(); file_put_contents('test.txt', 'time: '. time() .'; abroted:'. $isAborted .'; status: '. $status); if (0 !== $status || $isAborted) { break; } sleep(2);}
You can try to comment out this sentence
// Echo str_repeat ('', 65536 );
In addition
Set_time_limit (0 );It is best not to use0
Http://www.bkjia.com/PHPjc/1036923.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1036923.htmlTechArticlePHP in Nginx active disconnect Connection Close and ignore_user_abort background run these two days get a PHP call SVN synchronization update multiple server update programs, in order to avoid com...