使用 PHP ob_start() 与 Apache Deflate/Gzip 压缩内容?
大多数网站都希望压缩其内容以节省带宽。然而,当涉及到运行 PHP 的 apache 服务器时,有两种方法可以做到这一点 - 使用 PHP 或使用 apache。那么哪一个在您的服务器上更快或更容易呢?
例如,在 PHP 中,我在页面开头运行以下函数来启用它:
/**
* Gzip compress page output
* Original function came from wordpress.org
*/
function gzip_compression() {
//If no encoding was given - then it must not be able to accept gzip pages
if( empty($_SERVER['HTTP_ACCEPT_ENCODING']) ) { return false; }
//If zlib is not ALREADY compressing the page - and ob_gzhandler is set
if (( ini_get('zlib.output_compression') == 'On'
OR ini_get('zlib.output_compression_level') > 0 )
OR ini_get('output_handler') == 'ob_gzhandler' ) {
return false;
}
//Else if zlib is loaded start the compression.
if ( extension_loaded( 'zlib' ) AND (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE) ) {
ob_start('ob_gzhandler');
}
}
其他选项是使用 Apache deflate 或 gzip (两者都是 非常接近)。要启用它们,您可以将类似的内容添加到 .htaccess 文件中。
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/x-httpd-php
由于 PHP 是一种脚本语言(必须由 PHP 加载),我认为 apache 方法会 1)更稳定并且 2)更快。但假设在现实世界中没有多大用处。
毕竟,您会认为拥有巨大的财务支持窗口......呃,我们不会去那里。
Most sites want to compress their content to save on bandwidth. However, When it comes to apache servers running PHP there are two ways to do it - with PHP or with apache. So which one is faster or easier on your server?
For example, in PHP I run the following function at the start of my pages to enable it:
/**
* Gzip compress page output
* Original function came from wordpress.org
*/
function gzip_compression() {
//If no encoding was given - then it must not be able to accept gzip pages
if( empty($_SERVER['HTTP_ACCEPT_ENCODING']) ) { return false; }
//If zlib is not ALREADY compressing the page - and ob_gzhandler is set
if (( ini_get('zlib.output_compression') == 'On'
OR ini_get('zlib.output_compression_level') > 0 )
OR ini_get('output_handler') == 'ob_gzhandler' ) {
return false;
}
//Else if zlib is loaded start the compression.
if ( extension_loaded( 'zlib' ) AND (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE) ) {
ob_start('ob_gzhandler');
}
}
The other option is to use Apache deflate or gzip (both which are very close). To enable them you can add something like this to your .htaccess file.
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/x-httpd-php
Since PHP is a scripting language (which must be loaded by PHP) I would assume that the apache method would be 1) more stable and 2) faster. But assumptions don't have much use in the real world.
After all, you would assume that with the huge financial backing windows has... uh, we won't go there.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们正在运行...很多网络服务器,每天处理 60M/uniques。通常这不值得一提,但你的问题似乎是基于经验。
我们在 apache 中运行它。无论您选择哪种方法,另一端的输出都是相同的(或足够接近,因此不重要)。
我们选择 apache 的原因如下:
警告一下,某些浏览器或其他应用程序会故意破坏客户端标头,以表明支持压缩。有些人这样做是为了减轻客户端安全方面的工作(想想诺顿互联网安全等应用程序)。您可以忽略这一点,或者尝试添加额外的情况来重写请求,使其看起来正常(浏览器确实支持它,应用程序或代理只是对其进行了模糊处理以使自己的生活更轻松)。
或者,如果您使用flush()命令提前将输出发送到浏览器,并且您正在应用压缩,则可能需要用空格填充字符串的末尾,以说服服务器提前发送数据。
We're running... a lot of webservers, handling 60M/uniques/day. Normally this isn't worth mentioning but your question seems based on experience.
We run with doing it in apache. What comes out the other end is the same (or near enough so as to not to matter) regardless of the method you choose.
We choose apache for few reasons:
One word of warning, some browsers or other applications purposefully mangle the client headers indicating that compression is supported. Some do this to ease their job in terms of client side security (think applications like norton internet security and such). You can either ignore this, or try to add in extra cases to re-write requests to look normal (the browsers do support it, the application or proxy just futzed it to make its own life easier).
Alternatively, if you're using the flush() command to send output to the browser earlier, and you're applying compression you may need to pad the end of your string with whitespace to convince the server to send data early.