使用 PHP ob_start() 与 Apache Deflate/Gzip 压缩内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1862641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 18:28:47  来源:igfitidea点击:

Compressing content with PHP ob_start() vs Apache Deflate/Gzip?

phpapachegzipcompression

提问by Xeoncross

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 PHPor with apache. So which one is faster or easier on your server?

大多数网站都希望压缩其内容以节省带宽。然而,当谈到运行 PHP 的 apache 服务器时,有两种方法可以做到——使用 PHP或使用 apache。那么哪个在您的服务器上更快或更容易?

For example, in PHP I run the following function at the start of my pages to enable it:

例如,在 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');
    }

}

The other optionis to use Apache deflate or gzip(both which are very close). To enable them you can add something like this to your .htaccess file.

另一种选择是使用Apache放气或gzip的(这两者都是非常接近)。要启用它们,您可以在 .htaccess 文件中添加类似的内容。

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.

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

After all, you would assume that with the huge financial backing windows has... uh, we won't go there.

毕竟,你会假设有巨大的资金支持窗口......,我们不会去那里。

采纳答案by preinheimer

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

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

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 有以下几个原因:

  • Zero maintenance, we just turned it on. No one needs to maintain some case structure
  • Performance, in our tests servers where Apache did the work faired marginally better.
  • Apache will apply the output filter to everything, as opposed to just PHP. On some occasions there are other types of content being served on the same server, we'd like to compress our .css and .js
  • 零维护,我们刚刚开启。没有人需要维护一些案例结构
  • 性能,在我们的测试中,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).

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

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.

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