One of the things people tend to forget is the ability for web servers to compress content before sending it back to client. Client’s browser then uncompresses the data and displays it to the user. Pretty much all of the recent browsers support gzip compression. In this post, I will go over how to setup apache2 to use compression. First let’s see if your Apache installation has “deflate” enabled. You can check to see if you have deflate by typing:
# /usr/local/apache2/bin/apachectl -t -D DUMP_MODULES
If you don’t have have deflate_module, you would have to recompile your apache with “–enable-deflate” option.
Going forward, I am going to assume you have deflate_module. Add the following to your apache conf file:
BrowserMatch ^Mozilla/4\.0 no-gzip\
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
# Don't compress images
SetEnvIfNoCase Request_URI \
\.(?:gif|jpe?g|png)$ no-gzip dont-vary
The main thing you need to configure is the line which says “no-gzip dont-vary” also in bold above. This tells apache to not compress certain type of files. I have noticed on some of my sites that swf (flash) files do not work as expected if they are compressed. So if you have swf files in your site, you may want to add |swf right after png.
This is all what it takes for you to enable gzip compression in Apache2. Once you restart your apache so it reads the conf file, you can test if your site is getting compressed or not by using this tool: http://www.gidnetwork.com/tools/gzip-test.php
Here are the results for my blog:
Results for: http://crazytoon.com
Web page compressed? Yes
Compression type? gzip
Size, Markup (bytes) 57,337
Size, Compressed (bytes) 11,666
Compression % 79.7
DISCLAIMER: Please be smart and use code found on internet carefully. Make backups often. And yeah.. last but not least.. I am not responsible for any damage caused by this posting. Use at your own risk.
Even though on-the-fly compression will help you in the short term, you’ll soon hit CPU limits if your site will start to grow.
We stricly use gzip for pages which can be cached for a couple of minutes, pre-generate the pages pre-compressing them. So those pages will only be compressed every 5 minutes or so instead of for every request.
Just remember that the mod_deflate rules are virtualhost specific! They are NOT system wide.
Evert is also somewhat correct BUT with todays 8core cpus memory management is where you have to worry the most not cpu on a apache server.
Freddy, when you say memory management is ‘where you have to worry’, what do you mean specifically? Do you just mean running out of memory (and having to swap) or something else like being limited by internal RAMCPU bandwidth?
Plus you do still have to worry a lot about CPU these days if you’re doing anything reasonably complex/dynamic.
8 cores just mean it takes longer before you hits this bottleneck, so thats a non-argument. For low-traffic sites, such as a blog, its of course fine =)
Pingback: Decreasing bandwidth usage of my blog | TheElitist
thank you very mach..
Thanks for the advice, I ended up using Gzip also.
Pingback: Kari’s World » Blog Archive » Running with breaks on
most internet providers now have compression settings.
I wanted to thank you for this very good read!!
I certainly loved every little bit of it.
I have got you saved as a favorite to check out new things
Wonderful site. Plenty of helpful information
here. I’m sending it to several buddies ans additionally sharing
in delicious. And obviously, thanks for your effort!
I precisely needed to thank you so much yet again. I do not know what I would’ve made to happen in the absence of the type of secrets provided by you directly on such question. It seemed to be an absolute frightful issue in my opinion, nevertheless discovering a specialised approach you processed it made me to leap over fulfillment. I will be happier for the assistance and as well , wish you comprehend what an amazing job you happen to be carrying out training the mediocre ones using a blog. I know that you’ve never met any of us.
I must voice my love for your kindness giving support to women who really want help with this issue. Your special commitment to passing the solution all around became extremely effective and has specifically encouraged individuals much like me to achieve their ambitions. Your amazing valuable useful information indicates a whole lot a person like me and even further to my fellow workers. Many thanks; from everyone of us.
I wish to express some appreciation to this writer just for bailing me out of this instance. As a result of looking out throughout the world wide web and getting solutions which are not pleasant, I was thinking my entire life was done. Living without the answers to the issues you’ve fixed all through the website is a critical case, as well as ones that would have in a wrong way affected my career if I hadn’t come across your site. Your actual ability and kindness in playing with all things was very useful. I’m not sure what I would have done if I had not come upon such a step like this. I can now look ahead to my future. Thanks for your time very much for your impressive and amazing guide. I will not hesitate to suggest your blog to any person who would like direction about this matter.
I want to express my appreciation to you for rescuing me from this particular situation. Just after browsing through the world wide web and meeting strategies that were not productive, I assumed my life was well over. Being alive devoid of the answers to the difficulties you’ve solved through your main posting is a serious case, as well as the kind which could have in a negative way affected my career if I had not noticed your web blog. Your primary natural talent and kindness in dealing with all the things was excellent. I’m not sure what I would’ve done if I had not come across such a step like this. I am able to at this point look forward to my future. Thanks a lot so much for this reliable and sensible help. I won’t hesitate to suggest the website to anyone who ought to have guide about this subject.