Apr 28 2009

Apache 101 - Compressing Files

Posted by Marcos Placona at 10:24 AM
2 comments
- Categories: VPS

I'm going to start a server configuration series here, where I'll be talking about my goals and frustrations when configuring my own webserver. As most of you know, I've been using my own VPS for the last two weeks, and promised to post everything about it here.

I won't be covering things like basic installation, or server set-up. there's tons of them all over the web, and I 'm sure most of them would beat anything I wrote here, as some of those guys are real *nix gurus, while I'm only a learner.

On this post, I'll be talking a little bit more about Apache2, and it's configurations; to start with, I'll talk about file compression.

The apache server is a really amazing web-server, that let's you configure almost everything, as well as create security rules.

Back to the topic, I'll describe here a few of the necessary steps to enable compression on your apache server.

Compression saves you a lot of server traffic, and helps to make sure pages are served more quickly. It adds a little bit more load to the server, as the server needs to deal with compressed files now; but this will be automatically compensated by the page loading time, and can be easily remediated with some server caching (which will be my next topic).

In earlier versions of apache, the compressing method used was GZIP. With the apache 2.x, the module has been replaced with mod_deflate, which is much easier to install, don't need special configurations.

Before we get started, I need to make it clear that this is how you would proced in a Debian based distro, and this might vary with other distros
The first thing we need to do, is install the beast on the server:

<code>a2enmod deflate </code>

We the restart apache in order to have the module loaded:

<code>/etc/init.d/apache2 restart</code>

I like to configure compression on a vhost basis instead of global basis, but it's really up to you. In my case, I simply open my vhost and update it.

<code>sudo nano /etc/apache2/sites-available/placona.co.uk</code>

I then simply add the following lines inside my <VirtualHost>

<code>
# compress a few file extensions
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/atom_xml
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE text/html
</code>

It pretty much says everything, but basically, what i;m doing here is telling mod_deflate to compress files with the specific MIME types.
The next section is really important, as it "tells" your server to negotiate the best way to serve files to different browsers. Some browsers cannot handle file compression, and will get crazy when receiving compressed files, so it's important the we also add the following lines right after our deflate commands:

<code>
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</code>

And that's all, simply restart your apache server again.

<code>/etc/init.d/apache2 restart</code>

Your files should now be served with 20 - 30% less volume than before. There's a nice way to check that; go to this url, and check your website performance. On the results, it should say if your files are compressed or not. After applying the changes described above, all the MIME common text mime types should be listed as compressed.

Possibly Related Links:



Comments

the_dude

the_dude wrote on 08/07/09 2:40 AM

do you have mod_deflate working with your coldfusion server? i've been having a tough time getting it to work with my Railo install...static files work fine, but i don't know what i'm doing wrong with the Railo pages...
Marcos Placona

Marcos Placona wrote on 08/07/09 12:03 PM

Hi, I have mod_deflate working on this very website, using the same procedure as described. What exactly can you not get to work?

cheers

Write your comment



(it will not be displayed)



Leave this field empty:







Related URLs

Subscribe

Categories

Search Archives

Monthly Archives