Apache Config for reduced bandwidth usage

Sunday, May 30th, 2010

This snippet of an Apache config (httpd.conf) attempts to implement an intelligent caching policy in order to reduce the amount of files needlessly served. Obviously you would only want to do this once your site has moved into test or production environment. Basic tests using YSlow firefox plugin show bandwidth usage reduced by almost 90%.

<VirtualHost *:80>
    ServerAdmin info@myhost.co.uk
    DocumentRoot /var/www/mysite/public_html/
    ServerName mysite.myhost.co.uk
    ErrorLog logs/mysite.myhost-error_log
    CustomLog logs/mysite.myhost-access_log common
    <Directory /var/www/mysite/>
      Options All
      AllowOverride All
      Order allow,deny
      Allow from all
    </Directory>
Header unset ETag
FileETag None

# Turn on Expires and set default to 0
ExpiresActive On
ExpiresDefault A0
 
# Set up caching on media files for 1 year (forever?)
<FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav)$">
ExpiresDefault A29030400
Header append Cache-Control "public"
</FilesMatch>
 
# Set up caching on media files for 1 week
<FilesMatch "\.(gif|jpg|jpeg|png|swf)$">
ExpiresDefault A604800
Header append Cache-Control "public"
</FilesMatch>
 
# Set up 2 Hour caching on commonly updated files
<FilesMatch "\.(xml|txt|html|js|css)$">
ExpiresDefault A14515200
Header append Cache-Control "proxy-revalidate"
</FilesMatch>
 
# Force no caching for dynamic files
<FilesMatch "\.(php|cgi|pl|htm)$">
ExpiresActive Off
Header set Cache-Control "private, no-cache, no-store, proxy-revalidate, no-transform"
Header set Pragma "no-cache"
</FilesMatch>

<Location />
# Compress output
AddOutputFilterByType DEFLATE text/html text/css text/plain text/xml application/x-javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</Location>
</VirtualHost>

You can leave a response, or trackback from your own site.

Tags:
Posted in: Development



Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>