当用户中止请求时,如何使PHP-FPM进程终止? (Nginx) [英] How do I get the PHP-FPM process to terminate when a user aborts request? (Nginx)
问题描述
我知道Nginx与PHP-FPM进程无关,但是我更希望如果用户中止而导致PHP-FPM进程死亡,那么它就不会继续做不必要的事情或浪费资源.对于PHP-FPM/Nginx,无论用户是否中止,trigger_error
都会发生:
I know Nginx has nothing to do with the PHP-FPM process, but I would much prefer if the PHP-FPM process died if a user aborts so it doesn't continue doing needless things or wasting resources. For PHP-FPM/Nginx the trigger_error
will happen regardless of user abort:
<?php
sleep(30);
trigger_error('Still happened?');
?>
如何使用户中止PHP-FPM? (如果可能的话)
How can I do user aborts for PHP-FPM? (if possible)
推荐答案
This is not implemented by php-fpm, more info here.
设置ignore_user_abort(FALSE)
仅在PHP通过套接字发送数据时有效,而在忙于计算响应时则无效.
Setting ignore_user_abort(FALSE)
only works when PHP sends data over the socket, not when it is busy calculating a response.
理想情况下,它应该在php-fpm中实现,但是鉴于PHP主要是单线程的,所以我认为这并不容易.
Ideally, it should be implemented in php-fpm, but I don't think it will be very easy, given that PHP is mostly single threaded.
在紧急情况下,您可以使用断开的连接杀死所有php-fpm进程.假设您有php-fpm在localhost:9000上侦听,这将起作用:
In emergency situations, you could kill all php-fpm processes with a broken connection. Assuming you have php-fpm listening on localhost:9000, this would work:
netstat -ntp | grep 127.0.0.1:9000 | grep php-fpm | grep CLOSE_WAIT |\
awk ' { print $7; } ' | cut -d/ -f1 | while read pid; do
echo "Killing php-fpm/$pid because client has closed connection"
kill $pid
done
这篇关于当用户中止请求时,如何使PHP-FPM进程终止? (Nginx)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!