php 刷新不工作
<?php
for($i=0;$i<20;$i++)
{
echo 'printing...<br />';
ob_flush();
flush();
usleep(300000);
}
?>
包含代码的网址: http://domainsoutlook.com/sandbox/delayed.php
我有一个专用服务器,这样我就可以进行更改。我正在运行 apache 和 nginx 作为代理服务器。
<?php
for($i=0;$i<20;$i++)
{
echo 'printing...<br />';
ob_flush();
flush();
usleep(300000);
}
?>
Url that contains the code: http://domainsoutlook.com/sandbox/delayed.php
I have a dedicated server so I can make the changes. I am running apache and nginx as the proxy server.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
这就是我发现的:
Flush 在 Apache 的 mod_gzip 或 Nginx 的 gzip 下不起作用,因为从逻辑上讲,它正在对内容进行 gzip 压缩,并且要做到这一点,它必须缓冲内容以对其进行 gzip。任何类型的 Web 服务器 gzip 压缩都会影响这一点。简而言之,在服务器端,我们需要禁用 gzip 并减小 fastcgi 缓冲区大小。所以:
在 php.ini 中:
。输出缓冲 = 关闭
。 zlib.output_compression = Off
在 nginx.conf 中:
。 gzip 关闭;
。 proxy_buffering off;
另外,请准备好以下几行,特别是如果您无法访问 php.ini:
@ini_set('zlib.output_compression',0);
@ini_set('implicit_flush',1);
@ob_end_clean();
set_time_limit(0);
最后,如果你有的话,注释下面的代码:
ob_start('ob_gzhandler');
ob_flush();
PHP测试代码:
So that's what I found out:
Flush would not work under Apache's mod_gzip or Nginx's gzip because, logically, it is gzipping the content, and to do that it must buffer content to gzip it. Any sort of web server gzipping would affect this. In short, at the server side, we need to disable gzip and decrease the fastcgi buffer size. So:
In php.ini:
. output_buffering = Off
. zlib.output_compression = Off
In nginx.conf:
. gzip off;
. proxy_buffering off;
Also have this lines at hand, specially if you don't have acces to php.ini:
@ini_set('zlib.output_compression',0);
@ini_set('implicit_flush',1);
@ob_end_clean();
set_time_limit(0);
Last, if you have it, coment the code bellow:
ob_start('ob_gzhandler');
ob_flush();
PHP test code:
您使用的是
ob_flush
而没有ob_start
,所以没有什么可以刷新的。它还取决于网络服务器和代理及其设置。
您应该禁用 Nginx 的缓冲(将
proxy_buffering off;
添加到配置文件并重新启动 Nginx)此外,检查您的 php.ini 是否包含
output_buffering = Off和
zlib.output_compression = Off
。You're using
ob_flush
withoutob_start
, so there is nothing to flush for it.It also depends on the webserver and proxy and its settings.
You should disable buffering for Nginx (add
proxy_buffering off;
to the config file and restart Nginx)Also, check if your php.ini contains
output_buffering = Off
andzlib.output_compression = Off
.主php文件;
它正在工作..
Main php file;
it's working..
您必须填充缓冲区,以便将其刷新到浏览器。
echo 完整代码后使用此
:
You have to fill the buffer, so that it can be flushed to browser.
Use this after echo
Complete code:
在 php.ini 中:
在 nginx.conf 中:
In php.ini:
In nginx.conf:
正如我所读到的,这似乎是一个非常难以解决的问题,我发现的唯一(肮脏)方法是编写一些无用的输出来填充 ≠ 缓冲区。
flush
。nginx 缓冲区可以降低到 PHP 标头大小
ob_flush
以具有与上面相同的行为NGX_SSL_BUFSIZE
在 nginx 编译中已修复这是我的 test.php 文件(使用
?size=...
调用它来更改在循环中写入空格)我可以设置的较低conf是
As I read, it seems a really hard problem to solve, and the only (dirty) way I found is writing something useless to output to fill the ≠ buffers.
flush
is needed.nginx buffers can be lowered until the PHP header size
ob_flush
need to be added to have the same behavior as aboveNGX_SSL_BUFSIZE
is fixed in nginx compilationHere is my test.php file (call it with
?size=...
to change space writing in the loop)And the lower conf I can set is
另一个可能的原因是 mod_security。看起来它有自己的缓冲区。因此,如果您正在使用它,您将必须设置:
有点肮脏的解决方法,但到目前为止,这是我让它工作的唯一方法。
Another possible cause is mod_security. It looks like it has it's own buffers. So if you are using it you will have to set :
Kind of a dirty workaround but so far that it the only way I got it to work.
只是想补充一下罗杰的答案。
添加
-flush参数,即
如果您在 Apache2 中使用 FastCGI php5-fpm 模块,您还必须确保在 Apache2 配置中
Just wanted to add to the Roger's answer.
If you are using FastCGI php5-fpm module within Apache2 you must also make sure you are adding
-flush
argument in your Apache2 configuration, i.e.
检查您的服务器 api
如果您在 CentOS 中找到您的服务器 api
,则在“/etc/httpd/conf.d/fcgid.conf”中添加此行
要测试,请重新启动您的 Apache 服务器并尝试以下代码
Check your server api with
If you found your server api
in CentOS then add this line in "/etc/httpd/conf.d/fcgid.conf"
To test, restart your Apache server and try below code
我注意到浏览器的反应有所不同。例如,Chrome 会永远保留输入,并且似乎并不关心是否提前显示它。
不出所料,如果应用上述提示(由其他 stackoverflowers 提供),Firefox 会更早地显示输入,因此请尝试使用 Firefox。
I have noticed that browsers react differently. Chrome, for example, holds on to the input forever, and doesn't seem to care about displaying it any earlier.
Unsurprisingly, Firefox will display the input earlier, if the above tips (contributed by other stackoverflowers) are applied, so try with Firefox.
我只能通过这种方式刷新 - 添加 session_write_close();
I was able to flush only this way - adding session_write_close();
我在 nginx 中使用 php-fpm 时尝试过很多次。许多答案只是指示:
在 php.ini 中:
在 nginx.conf 中:
但他们忘记了 nginx.conf 中非常重要的设置:
I have tried many times when using php-fpm with nginx. Many answers just instruct:
In php.ini:
In nginx.conf:
BUT they forgot the very important setting in nginx.conf:
这是唯一对我有用的没有填充的解决方案。
它给出了以下curl 输出:
在浏览器中看起来像这样:
This is the only solution that worked for me without the padding.
It gave the following output from curl:
It looks like this in the browser: