Monday, April 11, 2011

How can I pre-compress files with mod_deflate in Apache 2.x?

I am serving all content through apache with Content-Encoding: zip but that compresses on the fly. A good amount of my content is static files on the disk. I want to gzip the files beforehand rather than compressing them every time they are requested.

This is something that, I believe, mod_gzip did in Apache 1.x automatically, but just having the file with .gz next to it. That's no longer the case with mod_deflate.

From stackoverflow
  • This functionality was misplaced in mod_gzip anyway. In Apache 2.x, you do that with content negotiation. Specifically, you need to enable MultiViews with the Options directive and you need to specify your encoding types with the AddEncoding directive.

  • You can use mod_cache to proxy local content in memory or on disk. I don't know if this will work as expected with mod_deflate.

  • mod_gzip compressed content on the fly as well. You can pre-compress the files by actually logging into your server, and doing it from shell.

    cd /var/www/.../data/
    for file in *; do
        gzip -c $file > $file.gz;
    done;
    
    Otto : This will remove the original files, which means clients that don't have Aceept-Encoding: gzip won't be serviced.
    Aeon : good point, updated.
    Otto : While you're editing, why not add -9 and get the highest compression possible. My 1500 files compressed in 38 seconds, so it's worth doing to save every byte possible in bandwidth and download time. :) (Also wishing I could edit my typo in my previous comment. Ugh)
    Aristotle Pagaltzis : -9 is the default anyway.
    Otto : Not according to the man page on my Mac, it says -6 is the default.
  • To answer my own question with the really simple line I was missing in my confiuration:

    Options FollowSymLinks MultiViews
    

    I was missing the MultiViews option. It's there in the Ubuntu default web server configuration, so don't be like me and drop it off.

    Also I wrote a quick Rake task to compress all the files.

    namespace :static do
        desc "Gzip compress the static content so Apache doesn't need to do it on-the-fly."
        task :compress do
         puts "Gzipping js, html and css files."
         Dir.glob("#{RAILS_ROOT}/public/**/*.{js,html,css}") do |file|
          system "gzip -c -9 #{file} > #{file}.gz"
         end
        end
    end
    
  • I have an Apache 2 built from source, and I found I had to modify the following in my httpd.conf file:

    Add MultiViews to Options:

    Options Indexes FollowSymLinks MultiViews
    

    Uncomment AddEncoding:

    AddEncoding x-compress .Z
    AddEncoding x-gzip .gz .tgz
    

    Comment AddType:

    #AddType application/x-compress .Z
    #AddType application/x-gzip .gz .tgz
    
  • This is mostly working for me. But if I go to http://ismyblogworking.com/www.whatsthatbug.com to check http compression, there is one problem:

    "# Your blog page content type is application/x-gzip, not HTML or XHTML."

    This is causing a few people to get a download prompt instead of the compressed page. Do I need to use a Content-Type tag or something to fix this?

    EDIT: Nevermind, I think I just wasn't patient enough. It appears to be correct now.

  • I have the same issue in my Ubuntu 9.10 with apache. I have enabled mod_defleat but enable to support html.gz pages to server. I also add the Multiviews option in my virtualhost settings but still it ask me to download the file.

    any one can help me the exact settings

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.