<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule"
>
<channel>
	<title>G-Loaded Journal &#187; Compression</title>
	<atom:link href="http://www.g-loaded.eu/tag/compression/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.g-loaded.eu</link>
	<description>An open-source software and technology related journal</description>
	<lastBuildDate>Mon, 05 Dec 2011 19:55:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
		<item>
		<title>Use mod_deflate to Compress Web Content delivered by Apache</title>
		<link>http://www.g-loaded.eu/2008/05/10/use-mod_deflate-to-compress-web-content-delivered-by-apache/</link>
		<comments>http://www.g-loaded.eu/2008/05/10/use-mod_deflate-to-compress-web-content-delivered-by-apache/#comments</comments>
		<pubDate>Sat, 10 May 2008 00:09:24 +0000</pubDate>
		<dc:creator>George Notaras</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Administration]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[Compression]]></category>
		<category><![CDATA[HOWTO]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Servers]]></category>
		<guid isPermaLink="false">http://www.g-loaded.eu/?p=492</guid>
		<description><![CDATA[One of the most efficient methods to reduce the usage of bandwidth by the web server and, at the same time, increase the speed of the content delivery is to compress your web pages and, generally, all output that is returned to the clients. The compression of the web content can be done using several [...]]]></description>
			<content:encoded><![CDATA[<p>One of the most efficient methods to reduce the usage of bandwidth by the web server and, at the same time, increase the speed of the content delivery is to <strong>compress</strong> your web pages and, generally, all output that is returned to the clients. The compression of the <strong>web content</strong> can be done using several methods. This article describes how to use the <em>mod_deflate</em> module to compress Apache&#8217;s output <em>on-the-fly</em>.<br />
<span id="more-492"></span></p>
<h4>Introducing mod_deflate</h4>
<p>Apache prepares the response that will be sent back to the client in several stages. One of those stages involves the modification or conversion of the data using <strong>output filters</strong>. <em>mod_deflate</em>, once loaded and activated, inserts such a filter, named <strong>DEFLATE</strong>, in Apache&#8217;s chain of <em>output filters</em>, which compresses all data that goes through it according to some rules the web server administator has defined. For instance, one can set the compression level, restrict the compression to particular MIME types or prevent some problematic web browsers or other HTTP clients from receiving compressed data from the server.</p>
<p><em>mod_deflate</em> also offers an input filter which can be used to decompress compressed HTTP requests, but this feature is outside of the scope of the current document.</p>
<p>Here follow some instructions on how to configure <em>mod_deflate</em>. Most of it can be found inside HTTPd&#8217;s official documentation, so you&#8217;d better read this resource as well.</p>
<p>Note that all of the following configuration directives can be inserted in Apache&#8217;s <em>main server context</em> or can be saved to a file that will be loaded from within the main server or any other <em>virtual host context</em>. If the configuration directives are inserted in the main server context, then <strong>they will be inherited by all virtual hosts</strong>.</p>
<h4>Load mod_deflate</h4>
<p><em>mod_deflate</em> can be loaded like any other Apache module:</p>
<pre class="codesnp">
LoadModule deflate_module modules/mod_deflate.so
</pre>
<p>Please note that <strong>this directive can only exist in the main server configuration</strong>.</p>
<h4>Enable Compression</h4>
<p>The compression of the data can be enabled <strong>for all data</strong> that goes through the <em>DEFLATE</em> filter or <em>selectively</em> depending on its <strong>MIME type</strong>.</p>
<p>To enable the compression <strong>for any type of content</strong>, insert the following directive:</p>
<pre class="codesnp">
 SetOutputFilter DEFLATE
</pre>
<p>Alternatively, to define which filetypes should pass through the <em>DEFLATE</em> output filter use the  <strong>AddOutputFilterByType</strong> directive. The following is an example:</p>
<pre class="codesnp">
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
</pre>
<h4>Set the Compression Level</h4>
<p>Generally, the <a href="http://en.wikipedia.org/wiki/DEFLATE_(algorithm)">deflate compression algorithm</a> is fast enough, so setting the compression level to the maximum (9) will not cause any noticeable trouble, even to relatively old hardware.</p>
<pre class="codesnp">
DeflateCompressionLevel 9
</pre>
<h4>Custom Rules for problematic browsers</h4>
<p>The compression can be turned-off or be restricted to files of type <code>text/html</code> for known problematic web browsers. These are taken from the official documentation.</p>
<pre class="codesnp">
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</pre>
<h4>Keep track of the compression</h4>
<p>Finally you can keep track of the compression in order to evaluate the effectiveness of the use of <em>mod_deflate</em> in your server.</p>
<p>The following directives define some variables, such as:</p>
<ul>
<li><strong>instream</strong> : the size in bytes of the data as received by the <em>DEFLATE</em> filter.</li>
<li><strong>outstream</strong> : the size in bytes of the compressed data as returned from the <em>DEFLATE</em> filter.</li>
<li><strong>ratio</strong> : the compression ratio, <code>(Output/Input)x100</code></li>
</ul>
<pre class="codesnp">
DeflateFilterNote Input instream
DeflateFilterNote Output outstream
DeflateFilterNote Ratio ratio
</pre>
<p>Finally, you can define a custom <em>logformat</em> so to be able to record the aforementioned values to a logfile:</p>
<pre class="codesnp">
LogFormat '"%r" %{outstream}n/%{instream}n (%{ratio}n%%)' deflate
</pre>
<p>The deflate logformat can be used for the main server&#8217;s or for any vhost;s logfile.</p>
<h4>Effectiveness of Compression</h4>
<p>It is well known that not all document types can benefit the same from compression. Generally, the <em>deflate algorithm</em> can compress <strong>text</strong> surprisingly fast and with a very high efficiency ratio. On the other hand, it is almost useless when used to compress <strong>images</strong> which have been prepared for the web such as PNG, JPEG, GIF and generally all other image types in which the data has already been compressed. The same goes for compressed <strong>audio</strong> files, such as MP3, AAC, OGG, <strong>videos</strong>, <strong>PDF</strong> documents and all other already compressed files.</p>
<p>So, the benefits of using <em>mod_deflate</em> to reduce the bandwidth usage and speed up the content delivery are heavily dependent on the type of files your web server delivers.</p>
<h4>Browser Support</h4>
<p>A web server that sends compressed data to clients would be completely useless if the HTTP clients couldn&#8217;t decompress that data. All modern and popular web browsers support accepting content that has been compressed using the <em>gzip</em> or <em>deflate</em> algorithms, so there should be no problem at all.</p>
<h4>Appendix I</h4>
<p>Here is the complete <em>mod_deflate</em> configuration as described in this <span style="color:black;text-decoration:none;"><a href="http://www.g-loaded.eu/2008/05/10/use-mod_deflate-to-compress-web-content-delivered-by-apache/">article</a></span>. Save it in a file, named <strong>deflate.conf</strong> and import it in the main server&#8217;s configuration using the <strong>Include</strong> directive</p>
<p>(<code>Include /path/to/deflate.conf</code>):</p>
<pre class="codesnp">
#
# mod_deflate configuration
#
LoadModule deflate_module modules/mod_deflate.so
&lt;IfModule mod_deflate.c&gt;
        AddOutputFilterByType DEFLATE text/plain
        AddOutputFilterByType DEFLATE text/html
        AddOutputFilterByType DEFLATE text/xml
        AddOutputFilterByType DEFLATE text/css
        AddOutputFilterByType DEFLATE application/xml
        AddOutputFilterByType DEFLATE application/xhtml+xml
        AddOutputFilterByType DEFLATE application/rss+xml
        AddOutputFilterByType DEFLATE application/javascript
        AddOutputFilterByType DEFLATE application/x-javascript
        DeflateCompressionLevel 9
        BrowserMatch ^Mozilla/4 gzip-only-text/html
        BrowserMatch ^Mozilla/4\.0[678] no-gzip
        BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
        DeflateFilterNote Input instream
        DeflateFilterNote Output outstream
        DeflateFilterNote Ratio ratio
        LogFormat '"%r" %{outstream}n/%{instream}n (%{ratio}n%%)' deflate
&lt;/IfModule&gt;
</pre>
<p>This configuration will be <strong>inherited by all virtual hosts</strong>.</p>
<p>To <strong>disable</strong> it just comment out the line that loads the mod_deflate module (<code>#LoadModule ...</code>).</p>
<p>To record <em>mod_deflate</em>&#8216;s specific variable (<em>instream</em>, <em>outstream</em>, <em>ratio</em>) values for a virtual host, just add a new log file of type <em>deflate</em>:</p>
<pre class="codesnp">
CustomLog /path/to/vhost/logs/deflate_log deflate
</pre>
<p>This will give you an idea of how efficient is the use of mod_deflate in that particular vhost.</p>
<div class="cc-block"><em><a href="http://www.g-loaded.eu/2008/05/10/use-mod_deflate-to-compress-web-content-delivered-by-apache/">Use mod_deflate to Compress Web Content delivered by Apache</a></em>, unless otherwise expressly stated, is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/">Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License</a>. Terms and conditions beyond the scope of this license may be available at <a href="http://www.g-loaded.eu/about/disclaimer-and-license/">www.g-loaded.eu</a>.</div>
<h4>Related Articles</h4>
<ul><li><a href="http://www.g-loaded.eu/2006/12/04/optimize-and-compress-css-files/" rel="bookmark">Optimize and Compress CSS Files</a></li>
<li><a href="http://www.g-loaded.eu/2007/08/10/ssl-enabled-name-based-apache-virtual-hosts-with-mod_gnutls/" rel="bookmark">SSL-enabled Name-based Apache Virtual Hosts with mod_gnutls</a></li>
<li><a href="http://www.g-loaded.eu/2010/03/28/script-apache-error-report/" rel="bookmark">Script for Apache Error Report</a></li>
<li><a href="http://www.g-loaded.eu/2011/11/28/speed-up-apache-by-including-htaccess-files-into-httpd-conf/" rel="bookmark">Speed up Apache by including htaccess files into httpd.conf</a></li>
<li><a href="http://www.g-loaded.eu/2006/10/06/check-server-http-headers-with-curl/" rel="bookmark">Check Server HTTP Headers with CURL</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.g-loaded.eu/2008/05/10/use-mod_deflate-to-compress-web-content-delivered-by-apache/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
	</item>
	</channel>
</rss>

