Posted: April 26, 2009 | By: TJ | In Technology | No comments yet
If you’ve ever run YSlow to see how your site performs, you may have noticed the ETags component. Entity tags (ETags) are a mechanism that web servers and browsers use to determine whether the component in the browser’s cache matches the one on the origin server. ETags were added to provide a mechanism for validating entities that is more flexible than the last-modified date.
To set this up in Lighttpd, simply uncomment the mod_expire module (make sure this is listed before mod_compress) in the server.modules section of the lighttpd.conf file. Then down at the bottom of the file, add the following to enable ETags:
$HTTP["url"] =~ "(css|js|png|jpg|ico|gif)$" {
expire.url = ( "" => "access 7 days" )
}
etag.use-inode = "enable"
etag.use-mtime = "enable"
etag.use-size = "enable"
static-file.etags = "enable"
Then you’ll add another section to define the cache directory and file types to cache:
compress.cache-dir = "/var/cache/lighttpd/compress/"
compress.filetype = ("text/plain", "text/html", "application/x-javascript", "text/css")Create the directory defined in the compress.cache-dir value and chown the directory so Lighttpd can read and write to it (note: the owner and group may differ depending on what has been defined for the server.username and server.groupname in the lighttpd.conf:
$ mkdir /var/cache/lighttpd/compress/
$ chown www-data:www-data /var/cache/lighttpd/compress/
Save and restart Lighttpd:
$ /etc/init.d/lighttpd restart
You’ll likely want to set up a cron to empty the compress directory at a given interval depending on the needs of the application. I’ve included mine here:
00 */6 * * * find /var/cache/lighttpd/compress/ -type f -mmin +120 -exec rm -rf {} \;This cron for example will run every six (6) hours and clear out any file or directory that is more than (2) hours old. To test, simply curl an image or css file from the command line:
$ curl -I http://constantshift.com/wp-content/themes/constant-shift/style.css | grep Cache
You should see an output like this:
Cache-Control: max-age=604800