【发布时间】:2010-12-24 04:11:17
【问题描述】:
大多数网站都希望压缩其内容以节省带宽。然而,当涉及到运行 PHP 的 apache 服务器时,有两种方法可以做到这一点 - with PHP 或使用 apache。那么在您的服务器上哪一个更快或更容易?
例如,在 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)更快。但是假设在现实世界中没有多大用处。
毕竟,您会认为拥有庞大的财务支持窗口...呃,我们不会去那里。
【问题讨论】:
标签: php apache gzip compression