Home  >  Article  >  Backend Development  >  在Nginx和php-fpm的环境中有办法立即输出缓存区内容的吗?

在Nginx和php-fpm的环境中有办法立即输出缓存区内容的吗?

WBOY
WBOYOriginal
2016-06-06 20:38:50794browse

有如下代码:

<code>for($i=1;$i0,'msg'=>'这是测试的输出——'.$i));
        ob_flush();
        flush();
        sleep(1);
    }
</code>

按代码理解似乎应该是每过1秒钟,将输出一次json数据
而使用nginx作为web服务器时,会发现上面的代码,在程序运行10s后一次性输出了
php.ini中的output_buffering = 4096
后来又按网上说的尝试使用了ob_end_clean(); ob_implicit_flush(true);这些函数,但还是没有效果

回复内容:

有如下代码:

<code>for($i=1;$i0,'msg'=>'这是测试的输出——'.$i));
        ob_flush();
        flush();
        sleep(1);
    }
</code>

按代码理解似乎应该是每过1秒钟,将输出一次json数据
而使用nginx作为web服务器时,会发现上面的代码,在程序运行10s后一次性输出了
php.ini中的output_buffering = 4096
后来又按网上说的尝试使用了ob_end_clean(); ob_implicit_flush(true);这些函数,但还是没有效果

<code>flush();
ob_flush();
</code>

WAMP下:

<code>for($i=1;$i0,'msg'=>'这是测试的输出——'.$i));
        ob_flush();
        flush();
        sleep(1);
    }
</code>

我试了试,这样是可以的。
http://segmentfault.com/q/1010000000447596

Ngnix下:
关闭php.ini下的:zlib.output_compression = Off
关闭nginx.conf下的:gzip off
修改代码为echo str_pad(' ', 64000);

<code>function  test(){
    header("Content-Encoding: none\r\n");
    ini_set('zlib.output_compression', 'off');
    ob_start();
    for($i=0;$i";
        ob_flush();
        flush();
        sleep(1);
    }
}
</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn