大多数网站都希望压缩其内容以节省带宽。但是,当涉及到运行PHP的Apache服务器时,有两种方法可以实现 - with PHP或使用apache。那么你的服务器上哪一个更快或更容易?使用PHP ob_start()与Apache Deflate/Gzip压缩内容?
例如,在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');
}
}
的other option是使用Apache deflate or gzip(这两者都是very close)。要启用它们,你可以在你的.htaccess文件中添加这样的内容。
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/x-httpd-php
由于PHP是一种脚本语言(其必须由PHP加载)我假设apache的方法将是1)更稳定和2)更快。但是,假设在现实世界中没有多大用处。
毕竟,你会认为与巨大的财务支持窗口... 恩,我们不会去那里。
+1:我几乎不处理您的流量,但我更喜欢服务器处理它。有一个或两个注意事项:某些类型的声音文件不应被压缩,否则媒体播放器会窒息。这包括带有mp3文件的Windows Media Player。 – Powerlord 2009-12-07 21:35:19
虽然这个问题与媒体播放器8,所以它可能已经修复了。 – Powerlord 2009-12-07 21:35:52
通常我只压缩类型文本/ *的项目,然后我樱桃选择一些更多的内容类型(如应用程序/ JSON)来获取其余。 – preinheimer 2009-12-07 21:38:00