Page 1 of 1

Apache Mod Deflate tutorial

Posted: Mon Apr 02, 2012 10:26 pm
by nobody
I noticed that noone has discussed this topic here in the forum.

Many people (both admins and users) try to compress their websites to optimize the use of bandwidth and in order to speed up the loading proccess. Even google page speed online suggests to use compression. Many people try to do this via php but thats not very good. Best way is to do it directly from apache. This applies for anyone running apache.

I have been using it in my server for more than 6 months. It does work.
Just go to google page speed online and check for example a joomla site before and after you setup the mod deflate. I saw up to 25points difference !

First of all make sure that in your httpd.con you have loaded the mod deflate.
There shoudl be this line somewhere. If not add it !

Code: Select all

LoadModule deflate_module modules/mod_deflate.so
Ok

Next we will create the deflate.conf

Code: Select all

vi /etc/httpd/conf.d/deflate.conf

Next paste the following to the conf we have just created and save it.

Code: Select all

<IfModule mod_deflate.c>
#What types must be compressed
        AddOutputFilterByType DEFLATE text/plain
        AddOutputFilterByType DEFLATE text/html
        AddOutputFilterByType DEFLATE text/xml
        AddOutputFilterByType DEFLATE text/css
        AddOutputFilterByType DEFLATE application/x-httpd-php
        AddOutputFilterByType DEFLATE application/x-httpd-fastphp
        AddOutputFilterByType DEFLATE application/x-httpd-eruby
        AddOutputFilterByType DEFLATE image/svg+xml
        AddOutputFilterByType DEFLATE application/xml
        AddOutputFilterByType DEFLATE application/xhtml+xml
        AddOutputFilterByType DEFLATE application/rss+xml
        AddOutputFilterByType DEFLATE application/javascript
        AddOutputFilterByType DEFLATE application/x-javascript

#What it should not compress
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.pdf$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.avi$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.mov$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.mp3$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.mp4$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.rm$ no-gzip dont-vary

#Maximum compression level (even old hardware have no problem with that...)
        DeflateCompressionLevel 9

#Handle browsers with known issues
        BrowserMatch ^Mozilla/4 gzip-only-text/html
        BrowserMatch ^Mozilla/4\.0[678] no-gzip
        BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

#Logging
        DeflateFilterNote Input instream
        DeflateFilterNote Output outstream
        DeflateFilterNote Ratio ratio
        LogFormat '"%r" %{outstream}n/%{instream}n (%{ratio}n%%)' deflate
</IfModule>

Restart apache

Code: Select all

service httpd restart

Done !

Re: Apache Mod Deflate tutorial

Posted: Tue Apr 03, 2012 3:26 pm
by mikeshinn
Thanks for the great tutorial!

Re: Apache Mod Deflate tutorial

Posted: Tue Apr 03, 2012 4:25 pm
by scott
Great work, we're trying all this out on the atomic servers right now

Re: Apache Mod Deflate tutorial

Posted: Tue Apr 03, 2012 6:11 pm
by nobody
You are welcome guys. You have helped me a million times. I am happy I was able to help once too :)

Re: Apache Mod Deflate tutorial

Posted: Tue Apr 03, 2012 6:59 pm
by mikeshinn
And if you want to test to see if your server is compressing content, you can use this site to do it:

http://www.whatsmyip.org/http-compression-test/

Re: Apache Mod Deflate tutorial

Posted: Tue May 15, 2012 6:01 pm
by nobody
scott wrote:Great work, we're trying all this out on the atomic servers right now
Hey guys.
I didn't see any feedback so I wanted to ask. Did you see good results ? Maybe you did any configuration adjustments or added lines within the deflate.conf ?

Re: Apache Mod Deflate tutorial

Posted: Thu May 17, 2012 10:58 am
by faris
mod_deflate increases CPU usage -but it is always a good idea to use it even so. I've been using it for years. There are no downsides as such, as long as the config is good (and there are various ways of doing it -- I like the one here, which is similar to mine).

Re: Apache Mod Deflate tutorial

Posted: Thu May 17, 2012 11:29 am
by JnascECSI
I started using it last month and as mentioned the cpu increase was up but it did make all the sites exspecially the joomla ones more snappy... :D

Re: Apache Mod Deflate tutorial

Posted: Thu May 17, 2012 7:59 pm
by nobody
Statistically the cpu goes up but only by an average of 2-3%.
With todays CPU's this increase is close to nothing. But on the other hand sites load faster and i think thats great.
Any suggestions for the conf though guys ? Maybe we can enhance this confguration more and make it even better !

Re: Apache Mod Deflate tutorial

Posted: Fri May 18, 2012 6:20 am
by faris
As I recall, there's two basic ways to set up mod_deflate:

Compress all by default, then define exceptions
Don't compress all by default, and define what to compress (with exceptions)

There's lots of things you don't want to compress:

Anything above a certain size, possibly (but I don't think this is an option in the config)

Every type of video file, possibly? (e.g. add .mkv in particular, plus possibly .ts, .mpg, .mpeg although some of those might benefit. But do we want to? They are likely to be big, and reducing a 10Mb file to 9Mb isn't worth my CPU cycles, I don't think.

What about Flash - .swf? .flv?

Audio files? .ogg, for example is nicely compressed to start with. But again some of those are compressible but may be big and not worth the bother.


Anyway here's an alternative mod_deflate.conf, which appears to be from the "deflate all, except as defined" school, and comes from the Apache website ( http://httpd.apache.org/docs/2.0/mod/mod_deflate.html ) with a modification to comment out the cludge for 2.0.48 on the assumption that people will be using a later version)

Code: Select all

<Location />
 # Insert filter
 SetOutputFilter DEFLATE

 # Netscape 4.x has some problems...
 BrowserMatch ^Mozilla/4 gzip-only-text/html

 # Netscape 4.06-4.08 have some more problems
 BrowserMatch ^Mozilla/4\.0[678] no-gzip

 # MSIE masquerades as Netscape, but it is fine
 # BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

 # NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
 # the above regex won't work. You can use the following
 # workaround to get the desired effect:
 # BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

 # Don't compress images
 SetEnvIfNoCase Request_URI \
 \.(?:gif|jpe?g|png)$ no-gzip dont-vary

 # Make sure proxies don't deliver the wrong content
 Header append Vary User-Agent env=!dont-vary
 </Location>