COM pressing内容与PHP ob_start()与Apache的放气/ Gzip已? [英] Compressing content with PHP ob_start() vs Apache Deflate/Gzip?

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

问题描述

大多数网站想COM preSS他们的内容,以节省带宽。然而,当涉及到运行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');
    }

}

的<一个href=\"http://stackoverflow.com/questions/388595/why-use-deflate-instead-of-gzip-for-text-files-served-by-apache\">other选项​​是使用Apache 放气或gzip压缩(这两者都是<一个href=\"http://stackoverflow.com/questions/883841/why-do-real-world-servers-$p$pfer-gzip-over-deflate-encoding/1579506#1579506\">very接近)。为了让他们,你可以添加这样的事情.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.

推荐答案

我们正在运行...很多Web服务器,处理60M /唯一身份用户/天。通常,这是不值得一提,但你的问题似乎基于经验。

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.

我们选择的Apache几个原因:

We choose apache for few reasons:


  • 零维护,我们只是把它。没有人需要保持一定的情况下,结构

  • 性能,在阿帕奇做我们的测试服务器上的工作流线型稍微好一些。

  • Apache将输出滤波器适用于一切,而不仅仅是PHP。在某些情况下也有在同一台服务器上提供的其他类型的内容,我们想COM preSS我们的.css和.js文件

提醒一句,一些浏览器或其他应用程序故意裂伤表明COM pression支持和客户端头。有人这样做是为了减轻他们的客户端安全性方面的工作(想想应用,如诺顿网络安全特警等等)。您可以忽略这一点,或试图在额外增加的情况下,重新写请求看起来正常(在浏览器都支持它,应用程序或代理只是futzed它来使自己的生活更轻松)。

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

另外,如果您使用的冲水()命令输出发送到更早的浏览器,你申请COM pression您可能需要您的垫字符串空白年底到服务器说服早发送数据。

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.

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

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