本文介绍了在Apache/Nginx设置中刷新输出缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让正在开发的网页的页面内容在下载时出现在屏幕上.在我的测试/开发环境中,可以使用PHP flush()命令按预期工作.

I would like to have page content for a web page I am developing appear on screen as it is downloaded. In my test/development environment this works as expected using the PHP flush() command.

但是,我的生产设置(WPEngine)在Apache前面使用了Nginx代理,并且flush()不再起作用(其他任何输出缓冲命令也不起作用).当我要发送4k的空格进行刷新时,可以通过故意填充缓冲区来获得所需的行为.

However, my production setup (WPEngine) uses an Nginx proxy in front of Apache and flush() no longer works (nor do any of the other output buffering commands). I have been able to get the desired behaviour by deliberately filling up the buffer when I want to flush by sending 4k worth of whitespace.

但是,这感觉像是一种黑客行为,所涉及的页面需要刷新100次或更多次,这样才能为下载的总数据增加可观的数量.

However, that feels like a hack and the page in question needs to be flushed 100 times or more so this adds a considerable amount to the total data downloaded.

是否可以通过发送控制字符和/或设置HTTP标头来通知Nginx刷新缓冲区(或根本不缓冲区),从而避免发送不必要的空白?

Is there a way to signal to Nginx to flush the buffer (or not buffer at all) by sending control characters and/or setting HTTP headers so I can avoid sending otherwise unnecessary whitespace?

由于WPEngine是托管主机环境,所以我无法对服务器设置进行任何更改.因此,例如,不能通过向nginx服务器配置中添加指令来关闭Nginx缓冲.

Since WPEngine is a managed hosting environment, I am not able to make any changes to the server setup. So, for example, turning off Nginx buffering by adding a directive to the nginx server config is not an option.

我目前的操作方式如下:-

The way I am currently doing this is as follows:-

<?php
//turn off server content compression for this page
header('Content-Encoding: none;');

//turn off PHP output buffering
ob_end_flush();

//make padding to fill buffer
$buffer = str_repeat(" ", 4096*8);

$start = time();

do
{
    printf( 'Time: %s secs<br>', time() - $start );
    echo $buffer;
    sleep(1);
} while( (time() - $start) < 10 );
?>

推荐答案

您应关闭nginx中的缓冲:

You should turn off buffering in nginx:

proxy_buffering off;

参考: http://nginx.org/r/proxy_buffering

这篇关于在Apache/Nginx设置中刷新输出缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 02:26
查看更多