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

查看:96
本文介绍了使用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 defflate或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


$ b <我假设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.

毕竟,你会认为,随着巨大的财务支持窗口... uh ,我们不会去那里。 p>

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天全站免登陆