Saturday, 10 January 2009

4 Simple Apache tweaks to speed up delivery of your web site

Add these simple directives into your Apache configuration and you should notice a considerable increase in speed in the delivery of pages. As a working example, I performed these tweaks on Here are the before and after stats:

The changes are:

  • Homepage on an empty cache from 956.6k to 658.9k, both 133 requests.
  • Homepage on a primed cache from 153.9k and 128 requests, to 25.9k and 8 requests.

Starting point for client side caching
To be added in the directive.

Adds a 1 week client side cache to GIFs, CSS, JS, SWF and JPEGs. Note once in place this cannot be revoked on the client end, the cache is in the user’s web browser. Be sure the site doesn’t overwrite images etc when using this, images need unqiue names.

Requires mod_expires (expires.load)
#Add client side caching for 604800 seconds
ExpiresActive On
ExpiresByType image/gif A604800
ExpiresByType text/css A604800
ExpiresByType application/x-javascript A604800
ExpiresByType image/jpeg A604800
ExpiresByType application/x-shockwave-flash A604800

If needed you can add more or custom MIME types for the ExpiresByType directive to process by editing /etc/mime.types and adding your own lines to the end.

Enable gzip
To be added in the directive.

Should be used on all sites. This used to effect render time, but PCs are plenty fast enough these days.

Requires mod_deflate (deflate.load) and mod_headers (headers.load)

#Add compression on everything except images, with netscape fix and make sure proxys don't override agent
SetOutputFilter DEFLATE
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE[E] !no-gzip !gzip-only-text/html
SetEnvIfNoCase Request_URI \
\.(?:gif|jpe?g|png)$ no-gzip dont-vary
Header append Vary User-Agent env=!dont-vary

Configure ETAGs
To be added in the directive.

This is best used on HTTP load balancers. It prevents the client seeing 2 pages that are identical on 2 servers as different pages and pulling unnecesarily. If this is added to a site on a single web server, HTML will never get cached increasing the load, but this can have good effects as well.

#Disable ETags to hide multiple servers and aid caching
FileETag none

Prefork Configuration
To be modified in the httpd.conf/apache2.conf directive.

The default configuration top’s out way to fast. Any modern server can handle the load this will allow.

ServerLimit 1024

StartServers 8
MinSpareServers 5
MaxSpareServers 20
MaxClients 1024
MaxRequestsPerChild 4000

Further Reading
Yahoo!’s performance rules:

No comments: