8

In Drupal 7 where would I be able to enable/disable gzip compression? Is there a module for this functionality?

9
  • 1
    If you want to enable compression when you save/serve cached pages, you first click on Cache pages for anonymous users and then save your options on your admin/config/development/performance page. This will then present a Compress cached pages. option further down in the BANDWIDTH OPTIMIZATION section (it is hidden/shown via javascript so this might all work upon the first click but doesn't here for some reason).
    – Jimajamma
    Commented Apr 2, 2014 at 17:04
  • its been checked off as compressed and im testing my site (xcubicle.com) with: whatsmyip.org/http-compression-test --- says its not compressed. not sure why. Commented Apr 2, 2014 at 17:33
  • But if I surf over to, say xcubicle.com/buy/20467/google-lg-nexus-5-1632gb-unlocked-phones it is, so this is suggesting that your home page isn't being cached and thus isn't being compressed by drupal
    – Jimajamma
    Commented Apr 2, 2014 at 17:40
  • i see i think culprit could be the form on the front page thats causing it not to cache and get gzipped. any solutions to that. Commented Apr 2, 2014 at 18:07
  • 5
    This question is actually very clear. This should not have been closed. Commented Jan 22, 2015 at 19:17

1 Answer 1

11

Personally, I do not like the way Drupal handles output compression; I take care of this outside of Drupal.

On the Drupal site, I add

$conf['page_compression'] = FALSE;
$conf['css_gzip_compression'] = FALSE;
$conf['js_gzip_compression'] = FALSE;

to settings.php, and this to a custom module to show that this is disabled:

/**
 * 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.');
}

This is to also prevent accidental double output compression, which can be very hard to diagnose if you don't know about the symptoms.

Then, in my Apache config, I do

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

This lets Apache do the output compression by MIME type, and also make sure all text based output gets compressed. This is adapted from an older version of the HTML5 Boilerplate project's .htaccess file, which now lives in a separate project. I also add in their directives for cache control, and a few other things. I keep all of this in an individual file, that I then Include in my virtual hosts.

The downside of this is that the server compresses each request, but it works well for my sites and my clients.

Not the answer you're looking for? Browse other questions tagged or ask your own question.