In the past two days, I have developed a PHP actively disconnects under Nginx Connection Close and ignore_user_abort run in the background_PHP tutorial actively disconnects under Nginx Connection Close and ignore_user_abort run in the background_PHP actively disconnects under Nginx Connection Close and ignore_user_abort run in the background_PHP tutorial tutorial program that calls SVN to synchronize update updates on multiple servers. In order to avoid being blocked during commit for a long time, I have to find a way to only request the trigger without waiting for the program update to complete and return the results. This is so time-consuming. It took too long, so I studied how to make PHP actively disconnects under Nginx Connection Close and ignore_user_abort run in the background_PHP tutorial actively disconnects under Nginx Connection Close and ignore_user_abort run in the background_PHP actively disconnects under Nginx Connection Close and ignore_user_abort run in the background_PHP tutorial tutorial actively disconnect. After working on it all afternoon, I found a lot of problems. Fortunately, I finally solved them. The main reason was that Nginx was too messy. .
<code class="hljs" java="">/** * 主动断开与客户端浏览器的连接 * 如果是 Nginx 服务器需要输出大于等于 fastcgi_buffer_size 缓存的数据才能即时输出 header 断开连接, 若还是不行可尝试关闭 gzip * 如: fastcgi_buffer_size 64k; 即: 需要 64*1024 字符(可多不可少), * 可使用 str_repeat(' ', 65536); 另外 str_repeat(' ', 6554); 这种方式其实生成速度更慢 * @param null|string $str 当前输出的内容, 若无需输出则设置为空 */ public function connectionClose($str = null) { $str = ob_get_contents() . $str; // 若实际输出内容长度小于该值将可能导致主动断开失败 header('Content-Length: '. strlen($str)); Header::connectionClose(); ob_start(); echo $str; ob_flush(); flush(); }</code>
There is generally no problem with apache. I used xampp on Windows to debug and found no problems at first. It turned out that it was Nginx on the server. It was dead or alive and crashed for an afternoon. It was later revealed that it was Nginx's fastcgi_buffer. question.
After testing N times under various conditions, there should be no bugs. . .
When the browser is closed, determine whether the program will continue to execute in the background. (In the example below, you do not have to set it to never timeout limit 0 when testing, just set it for one or two minutes, otherwise It may take a long time to restart the HTTP service)
To put it simply, if you want the program to continue executing after the user's browser is closed, then you must add the following code:
<code bash="" class="hljs">ignore_user_abort(true);</code>
But it is slightly different depending on the situation of your subsequent program (mainly the while loop):
Generally in the program you can monitor the connection status for control:
<code bash="" class="hljs">$isAborted = connection_aborted(); $status = connection_status(); if (0 !== $status || $isAborted) { break; }</code>
But there is a prerequisite for these two functions to work properly, that is, your program must have output content and be larger than the output cache of the current WebServer, so that it will work.
If you simply output a space echo ' '; it may have to loop thousands of times before it is judged, so in order to detect the status more immediately, you must output enough content each time it loops to trigger the status detection. .
So we often encounter a problem here: when the browser is disconnected, even if ignore_user_abort(true); is not used, the program will continue to execute because there is no output, and the infinite loop will continue to run. If it is set It's okay if it times out, otherwise you're really dead.
<code bash="" class="hljs">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); }</code>
You can try commenting out this sentence
// echo str_repeat(’ ‘, 65536);
Also
set_time_limit(0); It is best not to use 0