Apache mod_expires: an example

Good examples of server configurations are sometimes hard to find. Following is an implementation of mod_expires for a heavily trafficked, graphics intensive site.

First, the config for mod_expires. In this case in a file named /etc/httpd/conf.d/expires.conf:

# mod_expires enabled

  ExpiresActive on

  ExpiresByType text/javascript "access plus 1 hour"
  ExpiresByType application/x-javascript "access plus 1 hour"

  ExpiresDefault "access plus 1 seconds"
  ExpiresByType text/html "access plus 7 days"
  ExpiresByType image/gif "access plus 7 days"
  ExpiresByType image/jpeg "access plus 7 days"
  ExpiresByType image/png "access plus 7 days"
  ExpiresByType text/css "access plus 7 days"
  ExpiresByType text/javascript "access plus 7 days"
  ExpiresByType application/x-javascript "access plus 7 days"
  ExpiresByType text/xml "access plus 7 days"
  ExpiresByType application/json "now"

Here’s the companion mod_deflate configuration for the same site (in the file /etc/httpd/conf.d/deflate.conf):

# Configuration for mod_deflate
# If using Apache virtual hosts, read in to each vhost configuration with an
# Include statement, like "Include conf.d/deflate.conf".

  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
  AddOutputFilterByType DEFLATE application/json
  AddOutputFilterByType DEFLATE text/json

  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4.0[678] no-gzip
  BrowserMatch bMSIE !no-gzip !gzip-only-text/html

  SetEnvIfNoCase Request_URI .(?:gif|jp?g|png)$ no-gzip dont-vary

  Header append Vary User-Agent env=!dont-vary

  DeflateFilterNote Input inputstream
  DeflateFilterNote Output outputstream
  DeflateFilterNote Ratio ratio
  LogFormat '"%r" %{outputstream}n/%{inputstream}n (%{ratio}n%%)' deflate
  CustomLog logs/deflate_log deflate

  DeflateBufferSize 8096
  DeflateCompressionLevel 9
  DeflateMemLevel 9
  DeflateWindowSize 15

This entry was posted in System Administration on by .

About phil

My name is Phil Lembo. In my day job I’m an enterprise IT architect for a leading distribution and services company. The rest of my time I try to maintain a semi-normal family life in the suburbs of Raleigh, NC. E-mail me at philipATlembobrothersDOTcom. The opinions expressed here are entirely my own and not those of my employers, past, present or future (except where I quote others, who will need to accept responsibility for their own rants).