简体   繁体   English

在Nginx / PHP-FPM上使用PHP ob_flush刷新输出

[英]Flushing output with PHP ob_flush on Nginx/PHP-FPM

I'm trying to execute a PHP script which during execution provides the current status/percentage of completed actions. 我正在尝试执行一个PHP脚本,该脚本在执行过程中提供了当前状态/已完成操作的百分比。 For do that I call the script with AJAX and I use the ob_flush for send the output to the client during script execution, it worked on Apache but now I'm porting the project on Nginx and I need compatibility for both. 为此,我使用AJAX调用了脚本,并使用ob_flush在脚本执行过程中将输出发送到客户端,它在Apache上有效,但现在我将项目移植到Nginx上,并且我需要两者兼容。 On Nginx I use PHP-FPM for handle PHP files, here's the test script which I'm trying to run correctly: 在Nginx上,我使用PHP-FPM来处理PHP文件,这是我尝试正确运行的测试脚本:

//SET CORRECT CONTENT-TYPE (THEY WILL BE JSON STRINGS SEPARED BY BREAKLINE)
header('Content-type: text/plain; charset=utf-8');
//DISABLE GZIP FOR THE SCRIPT
ini_set('zlib.output_compression', 'Off');
ini_set('output_buffering', 'Off');
ini_set('output_handler', '');

ob_end_clean();
set_time_limit(0);

for ( $i = 0 ; $i < 5 ; $i++ ){
    echo "{\"code\":" . $i . "}\n";
    //SEND OUTPUT TO CLIENT
    flush();
    ob_flush();
    sleep(1);
}

The problem is that when I run this I get the output just when the script ends the execution, so after about 5 seconds. 问题是,当我运行此脚本时,我会在脚本结束执行时获得输出,因此大约需要5秒钟。 Someone can tell me what I'm doing wrong? 有人可以告诉我我在做什么错?

You should set HTTP response header 'X-Accel-Buffering' to 'no' when you don't want the Nginx to buffer the response from FastCGI server. 当您不希望Nginx缓冲来自FastCGI服务器的响应时,应将HTTP响应标头“ X-Accel-Buffering”设置为“ no”。

http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_buffering http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_buffering

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM