deflate.conf

The module mod_deflate will automatically send text files in a compressed format to web browsers that support it. This will often reduce the size of a file by 50-80%.

Enable the deflate module:

# a2enmod deflate

Create a generic configuration for deflate:

codetitle. /etc/apache2/deflate.conf

AddOutputFilterByType DEFLATE application/x-javascript text/html text/plain text/css text/javascript text/xml
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

Now we can just add Include /etc/apache2/deflate.conf to any VirtualHost or Location section we want to serve compressed files.

If you want to debug deflate, you can add this too:

codetitle. /etc/apache2/deflate.conf

DeflateFilterNote Input input_info
DeflateFilterNote Output output_info
DeflateFilterNote Ratio ratio_info
LogFormat '"%r" %{output_info}n/%{input_info}n (%{ratio_info}n%%)' deflate
CustomLog /var/log/apache2/deflate.log deflate
# touch /var/log/apache2/rewrite.log
# /etc/init.d/apache2 restart

expire.conf

The web browser will do its darnedest to cache static content when it can. However, it could use help. In our case, we have a lot of small avatar files with unique URLs: if the image changes, then the avatar will be different. So we would like to cache images for a very long time. The module mod_expire lets you specify how long certain content types should be cached for.

Enable mod_expire

# a2enmod expire

Create a generic configuration for mod_expire:

codetitle. /etc/apache2/expire.conf

<IfModule mod_expires.c>
  # turn on the module for this directory
  ExpiresActive on

  # cache common graphics for 3 days
  ExpiresByType image/jpg "access plus 3 days" 
  ExpiresByType image/gif "access plus 3 days" 
  ExpiresByType image/jpeg "access plus 3 days" 
  ExpiresByType image/png "access plus 3 days" 

  # cache css and javascript forever
  ExpiresByType text/javascript "access plus 10 years" 
  ExpiresByType application/x-javascript "access plus 10 years" 
  ExpiresByType text/css "access plus 10 years" 

  ExpiresDefault "access plus 24 hours" 
</IfModule>

To use this mod_expire configuration, put Include /etc/apache2/expire.conf in a Directory section in your apache config.

Caching forever for js and css is good in our case, because these assets will get a new url if they change.

gzip.conf

This config snippet will serve the .gz file instead of the uncompressed one, if it exists.

codetitle. /etc/apache2/gzip.conf

AddEncoding gzip .gz
RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{HTTP_USER_AGENT} !Safari
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule ^(.*)$ $1.gz [QSA,L]
  1. The first line tells the server that files with .gz extensions should be served with the gzip encoding-type, so the browser knows what to do with them.
  2. The second line checks that the browser will accept gzipped content — the following lines will not be executed if this test fails.
  3. We exclude Safari as it doesn’t interpret the gzipped content correctly. Is this still true? Most Safari versions are now 4+
  4. We check that this gzipped version of the file exists (fourth line)
  5. If all the prior checks pass, then we append .gz to the requested filename.