强制连接关闭lighttpd

问题描述 投票:0回答:1

我需要在输出发送到客户端之后在后台运行代码一段时间。但是连接会阻塞,直到作业完成才关闭。

查看:How do I close a connection early?https://www.php.net/manual/en/features.connection-handling.php#71172(和其他链接的答案)

我最终得到了代码:

    // credit : https://stackoverflow.com/questions/138374/how-do-i-close-a-connection-early#141026
    //          and related sources
    // broken on : lighttpd/1.4.53 - php/7.1.3

    $size=ob_get_length();
    header("Content-Length: $size",1);
    header("Connection: close",1);
    header("Content-Encoding: none\r\n");

    // flush all output
    ob_end_flush();
    ob_flush();
    flush();
    ob_end_clean();

    // if you're using sessions, this prevents subsequent requests
    // from hanging while the background process executes
    if (session_id()) session_write_close();

    ... some code for 10 seconds here

我都尝试过:server.max-keep-alive-idle = 0server.max-keep-alive-requests = 0摘自 :lighttpd force close connectionhttps://serverfault.com/questions/283377/force-connection-close-lighttpd(放入/etc/lighttpd/lighttpd.conf中)我的设置:lighttpd / 1.4.53PHP / 7.1.3Firefox 68.6.0esr(32位)树莓派4树莓派uname -a:Linux raspberrypi 4.19.97-v7l +#1294 SMP 1月30日星期四13:21:14 GMT 2020 armv7l GNU / Linux启用的lighttpd模块:10-accesslog.conf10-fastcgi.conf10-重写.conf15-fastcgi-php.conf90-javascript-alias.conf

php http-headers lighttpd httpconnection
1个回答
0
投票

如果您的代码在发送CGI响应后需要进行处理,则您的代码需要在单独的执行上下文中进行处理,因为CGI在发送CGI时已经完成了工作响应,然后lighttpd将向您的脚本发送一个SIGTERM信号。

发送CGI响应后运行代码的一种方法是在发送CGI响应后在后台运行代码。参见https://www.php.net/manual/en/function.posix-setsid.php

[另一种方法是让您的CGI将作业放入作业队列中,并有一个独立于CGI的独立守护进程来处理队列,例如使用Redis服务器。

© www.soinside.com 2019 - 2024. All rights reserved.