Vous êtes sur la page 1sur 2

Compression

woensdag 3 maart 2010
10:50

Overview

When you request a file like http://www.yahoo.com/index.html, your browser talks to a web server.
The conversation goes a little like this:
a. Browser: Hey, GET me /index.html
b. Server: Ok, let me see if index.html is lying around…
c. Server: Found it! Here’s your response code (200 OK) and I’m sending the file.
d. Browser: 100KB? Ouch… waiting, waiting… ok, it’s loaded.

But what if the if we could send a .zip file to the browser (index.html.zip) instead of plain old index.html, we’d save on bandwidth and download time. 
The browser could download the zipped file, extract it, and then show it to user.

The conversation goes a little like this:
a. Browser: Hey, can I GET index.html? I’ll take a compressed version if you’ve got it.
b. Server: Let me find the file… yep, it’s here. And you’ll take a compressed version? Awesome.
c. Server: Ok, I’ve found index.html (200 OK), am zipping it and sending it over.
d. Browser: Great! It’s only 10KB. I’ll unzip it and show the user.

How to implement compression

The tricky part of this exchange is the browser and server knowing it’s ok to send a zipped file over. 
The agreement has two parts
○ The browser sends a header telling the server it accepts compressed content (gzip and deflate are two compression schemes):  Accept‐Encoding: gzip, deflate
○ The server sends a response if the content is actually compressed: Content‐Encoding: gzip

The “Accept‐encoding” header is just a request by the browser.  The “good news” is that we can’t control the browser. 
It either sends the Accept‐encoding: gzip, deflate header or it doesn’t.   The implementation of compression is therefore soly a serverside job. 
If the server doesn’t send the content‐encoding response header, it means the file is not compressed (the default on many servers). 

Apache has two compression options : mod_deflate and mod_gzip

Choosing between mod_deflate and mod_gzip

Apache 2.x does not use mod_gzip any more, it uses mod_deflate instead. 
The main differences between the two is that mod_deflate is faster while mod_gzip compresses data slightly better.
Choosing between mod_deflate and mod_gzip is therefore a tradeoff between CPU and bandwith. 
Because CPU is often a bottleneck for high traffic sites the default module mod_deflate is prefered.

Applying compresssion on Win Apache 2.2 

Most images, music and videos are already compressed. Don’t waste time compressing them again.
In fact, you probably only need to compress the “big 3 HTML, CSS and Javascript.

○ Adjust conf/httpd.conf 
#
# Add the following modules 
LoadModule deflate_module modules/mod_deflate.so
LoadModule filter_module modules/mod_filter.so

○ Adjust  /conf/extra/httpd‐vhosts.conf

Fundamentals Pagina 1
….
<VirtualHost *:80>
….
#
#  FilterDeclare filter‐name [type]
#  Type CONTENT_SET is intended for operations concerned with packaging the contents, such as mod_deflate (which applies gzip compression).
FilterDeclare gzipping CONTENT_SET
#
# FilterProvider filter‐name provider‐name  [req|resp|env]=dispatch match
FilterProvider gzipping deflate resp=Content‐Type text/html
FilterProvider gzipping deflate resp=Content‐Type text/css
#'$' here is substring match, match both text/javascript application/x‐javascript
FilterProvider gzipping deflate resp=Content‐Type $javascript
...
<Location />

# 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

# Make sure proxies don't deliver the wrong content
Header append Vary User‐Agent env=!dont‐vary

# DO NOT USE AddOutputFilterByType, this directive is deprecated
# AddOutputFilterByType DEFLATE text/html text/plain text/xml text/javascript

# Use SetOutputFilter if you would like to zip everyting of this location
# SetOutputFilter DEFLATE
#
#insert the filter into the chain, by default at the end.
FilterChain +gzipping
….
</Location>
</VirtualHost>

Verify solution
○ Use firebug to verify Content‐Encoding: gzip is present as a response header.

Issues 
○ AddOutputFilterByType  is a deprecated directive.  As stated by documentation : Because of certain problems discussed below, this directive is deprecated. 
The same functionality is available using mod_filter. 
References
 http://httpd.apache.org/docs/2.2/mod/core.html

Caveats
○ Using compression increase the CPU load of het server
○ Compression is useful for HTML, CSS and javascript.  Not useful for already compressed images, video and music.
○ Do not use AddOutputFilterByType  but use mod_filter instead

References
○ http://httpd.apache.org/docs/2.0/mod/mod_deflate.html
○ http://betterexplained.com/articles/how‐to‐optimize‐your‐site‐with‐gzip‐compression/
○ http://blog.usweb.com/archives/mod_deflate‐vs‐mod_gzip‐comparing‐the‐two/
○ http://www.geekride.com/index.php/apache‐enable‐mod_deflate‐mod_gzip‐module‐compression‐site‐optimise/
○ http://code.google.com/intl/nl/speed/page‐speed/

Fundamentals Pagina 2

Vous aimerez peut-être aussi