You are here: Home / What do you need? / Help and documentation / Unix tricks and information / Apache, gzip compression and binary files

Apache, gzip compression and binary files

by Darrell Kingsley last modified Mar 13, 2014 02:02 PM
Apache (or rather certain clients, such as media players) has (have) some problems with file sizes and compressed/deflated files. Here's what to do about it.

We've set up Apache to deflate most web content it serves with gzip, to make file transmission faster with smaller file sizes. This is great for HTML files, CSS and JS files, but for binary files, such as images and media files, or PDFs it can cause problems.

For PDFs the problems are that Acrobat can't read PDFs that have been gzipped, so it must be turned off for them.

Images generally are compressed already so there's no point i.e. it just consumes CPU power pointlessly, and it may also cause problems with clients (such as browsers) not knowing the size of the file to display.

Media files are sent with "Transfer encoding: chunked" set in the header and no "Content length" entry in the headers. This is because Apache can't work out how large the files are once they have been compressed, so it send them chunked. Various players can't handle chunked data so they play the first chunk or few chunks, assuming that to be the entire file.

The following code sets up compression but not for the content types above. It should be added to /etc/httpd/conf/httpd.conf.

<Location />
    SetOutputFilter DEFLATE
    SetEnvIfNoCase Request_URI  \
        \.(?:gif|jpe?g|png)$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI  \
        \.(?:mp3|wav|wma|au|m4p|snd|mid|wmv|mpg|mpeg|mp4|qt|mov)$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI \.pdf$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI  \
        \.(?:exe|t?gz|zip|gz2|sit|rar)$ no-gzip dont-vary
</Location>

Useful links

http://articles.sitepoint.com/article/mod_deflate-apache-2-0-x

http://httpd.apache.org/docs/2.2/mod/mod_deflate.html

http://httpd.apache.org/docs/2.2/mod/core.html

Safari, gzip deflation and blank pages