早期刷新HTML文档-使用ini_set('zlib.output_compression','ON')? [英] Flushing the HTML document early - with ini_set( 'zlib.output_compression','ON')?

查看:174
本文介绍了早期刷新HTML文档-使用ini_set('zlib.output_compression','ON')?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是在我的php脚本将页面的其余部分缝合在一起时,尽早刷新网站标题部分,并在完成后将其发送.重要的是,将大块压缩后发送到浏览器. (我正在使用Apache/2.2和PHP/5.3.4)

My goal is to early flush the header part of my website while my php script is stitching the rest of the page together and sends it once its done. Important is that the chunks are sent compressed to the browser. (I am using Apache/2.2 and PHP/5.3.4)

现在,我正在尝试通过在PHP脚本中调用ini_set("zlib.output_compression","On")来实现此目的.但是,如果我在脚本中的任何地方都使用了flush(),即使在压缩结束时,压缩也将不再起作用.

Right now I am trying to achieve this by calling ini_set("zlib.output_compression", "On") in my PHP script. But if I use flush() anywhere in my script even at the end the compression won't work anymore.

问题是:

a)通过使用此方法,一旦达到此输出缓冲区的大小,zlib将刷新输出缓冲区并将压缩后的块发送到浏览器?

a) By using this method zlib will flush the output buffer and send the compressed chunk to the browser once the size of this output buffer is reached?

b)如果是这样,有什么方法可以通过只设置zlib的内部缓冲区大小来控制zlip何时发送我的块 not ?默认值为4KB.

b) If so is there any way to fine control when zlip will send my chunk not by just setting the internal buffer size of zlib? Default is 4KB.

c)有什么好的选择可以实现较早的压缩冲洗,而对于我想要冲洗的时间可以进行更精细的控制?也许我完全走错了路:)

c) Are there any good alternatives to achieve an early compressed flush maybe with more fine control regarding the time when I want to flush it? Maybe I am totally on the wrong path :)

推荐答案

自从我不得不在OB上使用zlib压缩以来,这已经是很长的时间了(稍后再详细介绍).但是,让我尝试说服您在PHP中的OB上关闭zlib压缩.首先,要有一点背景知识,以确保我们在同一页面上.

It's been a LONG time since i had to use zlib compression on OB (more on why later). However, let me try and convince you to turn OFF zlib compression on OB in PHP. First of all, a little background to ensure we are on the same page.

怎么做

每次php都会打印一些东西,没有OB,它将直接发送到apache,并从apache发送到浏览器.取而代之的是,使用OB时,输出在apache处停止,并等待直到数据被刷新(到浏览器)或脚本结束并且数据自动被刷新.通过将Apache缓冲到Web浏览器阶段,在生成页面时可以节省大量时间和资源.

Everytime php prints something, without OB it would be sent straight to apache and from apache to the browser. Instead, with OB, the output stops at apache and waits until the data is flushed (to the browser) or until the script ends and the data is flushed automatically. This saves quite a lot of time and resources when generating a page by buffering the Apache to Web Browser stage of the process.

为什么不在PHP中使用OB压缩

WHY NOT TO USE OB COMPRESSION IN PHP

为什么要使PHP压缩?完成此类任务(例如压缩js文件)应该是服务器的工作.您应该"为完全免费地处理php而要做的就是安装NGINX作为公众的前端.设置为反向代理非常容易,您甚至可以将其作为php和apache安装在SAME服务器上.

Why would you make PHP compress it? It should be the servers job to do such tasks (as well as compress js files for example). What you "should" do to drastically free apache to process php is to install NGINX as a front to the public. It's VERY easy to setup as a reverse proxy and you can even install it on the SAME server as php and apache.

因此将NGINX设置在端口80上,将apache设置为8080(并且仅允许nginx连接,但不要担心是否将其公开一段时间,因为它已经公开,并且非常适合绕过nginx进行调试,所以不会出现任何安全问题-但我建议您不要将其公开发布太长时间).然后使nginx反向代理到apache,缓存所有从apache卸载的静态文件(因为nginx代替了它们),这意味着apache可以执行更多的php请求,并且还可以使nginx进行输出压缩;)释放apache和php甚至可以更多请求.另外一个好处是,nginx还可以比Apache更快地提供静态文件,并且Nginx也使用更少的ram并可以处理更多的连接.

So set NGINX on port 80, put apache on say 8080 (and only allow nginx to connect, but don't worry if you leave it public for a little time as it was already public and great for debugging to bypass nginx so no security issues should rise - but i recommend you don't leave it public for to long). Then make nginx reverse proxy to apache, cache all static files which offloads that from apache (because nginx serves them instead) meaning apache can do more php requests, and also get nginx to perform OUTPUT COMPRESSION ;) freeing up apache and php to do even more requests. As an added benefit, nginx can also serve static files much faster than Apache and Nginx also uses much less ram and can handle much more connections.

即使是一名nginx新手,也可以在线阅读一些教程并在1天之内完成我刚刚说的所有内容,即可进行nginx设置.也可以花1天的时间.

Even an nginx newbie could get nginx setup after reading a few tutorials online and complete everything i just said within 1 day. 1 day well spent as well.

请记住,对于PHP到Apache,请保持KEEP输出缓冲为开,但在PHP上应关闭zlib压缩,而应在nginx上启用它.

Remember to KEEP output buffering ON however for PHP to Apache but turn zlib compression OFF on PHP and enable it instead on nginx.

这篇关于早期刷新HTML文档-使用ini_set('zlib.output_compression','ON')?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆