Web Application Optimization with Apache2

Overview

Listed below are a few items you may find helpful in optimizing your web delivery experience. It's always wise to create a clean test rig (without making it unrealistic) where you can easily change a single element to measure the change in performance. I've used YSlow from Yahoo as a guide for helping with optimizing with page delivery in addition to Apache Bench (ab), Canoo WebTest and Apache JMeter. Figure out what you actually want to test and use the tool you feel most comfortable with :)

Apache 2

If you're using apache2 to serve web content then some of the following might be of use.

ETags

A HTTP ETag is a HTTP 1.1 response header that is used by the client to determine whether an entity (e.g. image) is the same without having to re-download it. If you're using multiple apache instances to serve the same content you should change the default ETag configuration which unfortunately by default includes the local file inode of the entity, thus making the same entity have a different ETag depending on which server you request it from. To rectify this simply insert the following into your httpd.conf :
        FileETag MTime Size 
I'm assuming you're using something like rsync to synchronize the resources between the various servers, thus maintaining the same modified time (MTime in apache-config speak) and hence ETag for the same resource.

mod_deflate

mod_deflate is an apache module designed to gzip the response of various types of content using the zlib library. Unfortunately some browsers (IE6) have some difficulties with gzip'd response content so the configuration is a little more complex to avoid such situations. Add the following into your virtual host/default entry:
        SetOutputFilter DEFLATE
        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|jpe?g|png)$ no-gzip dont-vary
        Header append Vary User-Agent env=!dont-vary
As you can guess from the snippet above we're matching on certain browser strings to ensure we don't gzip or only gzip text/html for those 'special' cases. Gzip-ing your response increases your webserver CPU load but in cases where you are bandwidth limited to your client this works well and can show a significant improvement.

mod_expires

'Expires' is a HTTP/1.1 header which provides a date/time after which the client can consider the content stale. If a HTTP/1.1 client revisits a page it will not rerequest the locally cached elements that have not yet expired (unlike Last-Modified where it will use a HEAD request to ask the server if the resource has been modified). Typically there are a number of pages that may fall under this category - help, search, sitemap, terms & conditions etc. To serve Expires headers from apache you can add something like the following to your virtual host/default entry:
        ExpiresDefault "access plus 2 days"
        ExpiresActive On
In this instance apache will serve up a date of the file last access time plus two days. See mod_expires for more info on how to configure this module.

mod_cache

According to the Apache 2.2 Caching Guide mod_cache is now considered suitable for production use. In this instance I've opted to use some file caching to cache some static resources from an upstream server:
        CacheRoot /var/apache/cache/wiki
        CacheEnable disk /templates
        CacheEnable disk /scripts
        CacheEnable disk /images
        CacheDefaultExpire 3600
        CacheMaxExpire 3600
Here I've created the cache root directory /var/apache/cache/wiki and cache all requests that fall under /templates, /scripts or /images with an expiry of 1 hour. This is an effective way of cutting down traffic to upstream servers, many of which are not setup to serve static content that effectively. Ensure your apache user can write to the cache root!

Add new attachment

Only authenticated users are allowed to upload new attachments.
« This page (revision-9) was last changed on 24-03-2008 10:57 by Gus