Earlier today James Bennett shared his opinions on parts of the problems that users run into when trying to deploy modern web applications on traditional shared hosting providers such as Dreamhost and A Small Orange. The long-lived processes needed for modern web frameworks like Ruby on Rails and Django, are often killed after a short timeout.
I have experienced this problem myself, on this blog. This site is hosted by A Small Orange, a hosting provider that is actually listed as a Django friendly web host. The Django FastCGI process receives a termination signal after a very short time, which means that almost every request to this site needs to restart the process.
The result? Well, in practice I'm running Django as a good old CGI application. And what does that mean? Almost every visitor to this site has to wait a good 5 seconds before the first byte is returned.
Earlier this week I stumbled upon StaticGenerator, a simple tool that generates static pages from your Django models and URLs. So tonight I thought I'd give it a spin and see if it had any effect on my problem. And it sure did!
StaticGenerator generates index.html files in a directory tree corresponding to the URLs for every model object you throw at it. By adding a single line to my .htaccess and following the simple steps provided by StaticGenerator, this site is now blazingly fast!
Since my URLs are already nice and clean I added a simple rule: Only reroute to the Django FastCGI process if index.html at the given path does not exist. My .htaccess before:
RewriteRule ^/(media.*)$ /$1 [QSA,L,PT]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ django.fcgi/$1 [QSA,L]
And after:
RewriteRule ^/(media.*)$ /$1 [QSA,L,PT]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteRule ^(.*)$ django.fcgi/$1 [QSA,L]
Easy as pie!
Comments
is this why your 'about' and 'links' pages just display 'oops not found'?
Oops :)
The reason they display not found is that they're simply not there. My bad for rolling out an early template without the code to back it up. Luckily I have some time to work on my blog again this week :)