PHP-FPM fastcgi_finish_request靠谱吗?

标签 php fastcgi

fastcgi_finish_request(); 语句后在我的 php 脚本中实现了一些后处​​理后,我担心会出现一些差异。

看起来 PHP 没有在 fastcgi_finish_request 之后执行所有脚本。

在日志文件中,我找不到关于这部分的通知,没有警告,没有错误或违规行为。

是否有文档中未提及的使用 fastcgi_finish_request 的限制或提示?

最佳答案

调用 fastcgi_finish_request() 后任何显式或隐式输出刷新将导致退出 PHP 脚本,而不会出现任何警告或错误。换句话说,在调用 fastcgi_finish_request() 之后调用 flush() 的行为就像调用 exit() 而不是 flush() 一样。

Here's a PHP ticket记录行为。他们将其标记为“不是错误”。

下面是一些重现问题的代码:

function writestamp($case, $file){
  file_put_contents($file, "". $case . ": " . time() . "" . PHP_EOL, FILE_APPEND);
}

// Flush buffers and stop output buffering
while (@ob_end_flush());

// Destroy session, otherwise request can't be finished
if(session_status() > 1) session_destroy();

$file = tempnam(sys_get_temp_dir(), 'flushbug_');
echo 'Writing 4 timestamps to: '.$file;

// this one gets called
writestamp('1', $file);

// and this
register_shutdown_function('writestamp', '4', $file);

// finish the request
fastcgi_finish_request();

// as does this
writestamp('2', $file);

// but this one does NOT - calling flush() after fastcgi_finish_request() exits PHP without errors or warnings
flush();
writestamp('3', $file);

一个简单的解决方法是调用 ignore_user_abort(true)fastcgi_finish_request 调用之前或之后:

ignore_user_abort(true);
fastcgi_finish_request();

关于PHP-FPM fastcgi_finish_request靠谱吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14191947/

相关文章:

fastcgi - 连接被对等方重置 : FastCGI: comm with server aborted: read failed

php - FastCGI:已中止:select() 失败

php - 错误网关 NGINX 502 PHP-FPM fastcgi

php - 在 MySQL 中使用准备好的语句是否可以防止 SQL 注入(inject)攻击?

php - 如何使用流 stream_socket_client() 在 aruba 中打开 ssl 连接

php - 如何将自定义字段添加到 IPB 中的表单

c - 在 C 中使用 FastCGI 访问 PUT 或 POST 请求的主体

apache - CGI 脚本 (Perl) 在 Apache 下使用 fastcgi_module 永久运行

php - 将网站从 Windows 迁移到 Linux

php - SQL 查询出了什么问题?