Mon, 04 Feb 2008

"Static files" efficiency in Blosxom

Blosxom is a CGI script and, in its usual configuration, it considers everything under $url as a Blosxom request. This makes it awkward to serve up static files (for instance, images, movies or tarballs) from anywhere under the Blosxom root URL. You typically receive a cryptic message such as 'Error: I'm afraid this is the first I've heard of a "mp3" flavoured Blosxom. Try dropping the "/+mp3" bit from the end of the URL.'

There are a couple of plugins which attempt to deal with this - e.g. binary and static_file - but both have the disadvantage that the Blosxom script is still invoked for each static file. Of course you can put all your static files in a /images/ directory or similar outside the weblog, but keeping track of a large number of images then becomes cumbersome.

It must be better to let your webserver handle static files, a task for which it is optimised, rather than invoking a CGI for each image. For instance, in apache httpd.conf, if your blosxom script is named /usr/lib/cgi-bin/blosxom:

DocumentRoot /home/www/public_html
<Directory /home/www/public_html/blosxom>
Options Indexes
# If source document doesn't exist, feed the URL to Bloxsom
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /cgi-bin/blosxom/$1 [L,QSA]
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

Requires Apache mod_rewrite (run a2enmod rewrite). Of course this will bypass any Blosxom authentication or restrictions, and any remote user who can guess a filename under the Blosxom tree can retrieve it even if it's not linked from anywhere. You should make sure that no unwanted files are accidentally exposed in your Blosxom tree and that any source or passwords are held outside the Blosxom <Directory>.

This doesn't affect the static rendering of dynamic Blosxom pages, which are held in $static_dir and not served directly. Although if you do use static rendering, you could get even cuter with your rewrites - mail me if you have any further hints and I'll put them up.

Thanks to Gavin Carr on blosxom-users for the idea.