使用 PHP ob_start() 与 Apache Deflate/Gzip 压缩内容? [英] Compressing content with PHP ob_start() vs Apache Deflate/Gzip?

查看:26
本文介绍了使用 PHP ob_start() 与 Apache Deflate/Gzip 压缩内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大多数网站都希望压缩其内容以节省带宽.但是,对于运行 PHP 的 apache 服务器,有两种方法可以做到 - 使用 PHP 或使用 apache.那么,哪一个在您的服务器上更快或更容易?

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?

例如,在 PHP 中,我在页面开头运行以下函数以启用它:

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');
    }

}

其他选项 是使用 Apache deflate 或 gzip(两者都是 非常接近).要启用它们,您可以将这样的内容添加到您的 .htaccess 文件中.

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

由于 PHP 是一种脚本语言(必须由 PHP 加载),我认为 apache 方法将 1) 更稳定,2) 更快.但是假设在现实世界中没有多大用处.

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.

推荐答案

我们正在运行...大量的网络服务器,每天处理 60M/uniques.通常这不值得一提,但您的问题似乎是基于经验.

We're running... a lot of webservers, handling 60M/uniques/day. Normally this isn't worth mentioning but your question seems based on experience.

我们在 apache 中运行.无论您选择哪种方法,另一端的结果都是相同的(或足够接近以至于无关紧要).

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.

我们选择 apache 有几个原因:

We choose apache for few reasons:

  • 零维护,我们刚刚开启了它.没有人需要维护一些案例结构
  • 性能,在我们的测试服务器中,Apache 完成了这项工作.
  • Apache 会将输出过滤器应用于所有内容,而不仅仅是 PHP.在某些情况下,同一服务器上会提供其他类型的内容,我们希望压缩 .css 和 .js

一个警告,一些浏览器或其他应用程序故意破坏客户端标头,表明支持压缩.有些人这样做是为了减轻他们在客户端安全方面的工作(想想诺顿互联网安全等应用程序).您可以忽略这一点,或者尝试添加额外的情况来重新编写请求以使其看起来正常(浏览器确实支持它,应用程序或代理只是对其进行了模糊处理以使自己的生活更轻松).

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).

或者,如果您之前使用 flush() 命令将输出发送到浏览器,并且您正在应用压缩,您可能需要用空格填充字符串的末尾,以说服服务器尽早发送数据.

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.

这篇关于使用 PHP ob_start() 与 Apache Deflate/Gzip 压缩内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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