我个人不喜欢Drupal处理输出压缩的方式。我在Drupal之外负责此工作。
在Drupal网站上,我添加了
$conf['page_compression'] = FALSE;
$conf['css_gzip_compression'] = FALSE;
$conf['js_gzip_compression'] = FALSE;
到settings.php,然后到一个自定义模块以显示它已被禁用:
/**
* Implements hook_form_FORM_ID_alter().
*/
function MYMODULE_form_system_performance_settings_alter(&$form, $form_state) {
$form['bandwidth_optimization']['page_compression']['#default_value'] = 0;
$form['bandwidth_optimization']['page_compression']['#disabled'] = TRUE;
$form['bandwidth_optimization']['page_compression']['#description'] = t('Handled by Apache.');
}
这还可以防止意外的双输出压缩,如果您不了解症状,可能很难诊断。
然后,在我的Apache配置中
<IfModule mod_deflate.c>
# Force deflate for mangled headers developer.yahoo.com/blogs/ydn/posts/2010/12/pushing-beyond-gzipping/
<IfModule mod_setenvif.c>
<IfModule mod_headers.c>
SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding
RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
</IfModule>
</IfModule>
# HTML, TXT, CSS, JavaScript, JSON, XML, HTC:
<IfModule filter_module>
FilterDeclare COMPRESS
FilterProvider COMPRESS DEFLATE resp=Content-Type $text/html
FilterProvider COMPRESS DEFLATE resp=Content-Type $text/css
FilterProvider COMPRESS DEFLATE resp=Content-Type $text/plain
FilterProvider COMPRESS DEFLATE resp=Content-Type $text/xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $text/x-component
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/javascript
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/json
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/xhtml+xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/rss+xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/atom+xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/vnd.ms-fontobject
FilterProvider COMPRESS DEFLATE resp=Content-Type $image/svg+xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $image/x-icon
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/x-font-ttf
FilterProvider COMPRESS DEFLATE resp=Content-Type $font/opentype
FilterChain COMPRESS
FilterProtocol COMPRESS DEFLATE change=yes;byteranges=no
</IfModule>
<IfModule !mod_filter.c>
# Legacy versions of Apache
AddOutputFilterByType DEFLATE text/html text/plain text/css application/json
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE text/xml application/xml text/x-component
AddOutputFilterByType DEFLATE application/xhtml+xml application/rss+xml application/atom+xml
AddOutputFilterByType DEFLATE image/x-icon image/svg+xml application/vnd.ms-fontobject application/x-font-ttf font/opentype
</IfModule>
</IfModule>
这样,Apache可以按MIME类型进行输出压缩,并确保所有基于文本的输出都经过压缩。该文件改编自HTML5 Boilerplate项目的.htaccess文件的旧版本,该文件现在位于单独的项目中。我还添加了用于缓存控制的指令以及其他一些内容。我将所有这些保存在一个单独的文件中,然后将其保存Include
在我的虚拟主机中。
这样做的缺点是服务器会压缩每个请求,但对我的站点和我的客户来说效果很好。
Cache pages for anonymous users
,然后将选项保存在admin/config/development/performance
页面上。然后,这将Compress cached pages.
在该BANDWIDTH OPTIMIZATION
部分的下方显示一个选项(它通过javascript隐藏/显示,因此在第一次单击时就可以全部使用,但由于某种原因在这里不起作用)。