The content of this blog is archived. No comments may be made, and no further content will be published. Thank you for your ongoing readership! -Ryan
| « Lifehacking: the quest for efficiency | A backported whois client for Ubuntu 8.04 LTS (Hardy) » |
Conditional redirection with lighttpd: evolving my webtmp folder
Very often, I need to quickly host a file for use in a forum post or on IRC. My usual approach has been to mount a "temporary" directory from my web server on my desktop, over ssh. However, I recently started using Dropbox to keep my important files synced: with this, I can access my school work from school without having to resort to ssh heroics or remembering to carry a flash drive with me (let's not get started on keeping it synchronized). Dropbox offers 2GB of storage for free, and they include a handy Public folder you can use to share files. Nice.
My little temporary directory, in practice, isn't very temporary. I rarely delete anything from it; indeed, the oldest image is nearing two years old. I also keep throughput test files there, and I kinda need those to be on my server. However, I hate remembering multiple long URLs, especially ones like http://www.hoopycat.com/~rtucker/tmp/moooar.PNG and http://dl.dropbox.com/u/3597105/system-fullpower.png. I also want to be able to transparently move files around as my needs change. And, most importantly, I want my stuff under the hoopycat.com domain for maximum flexibility.
So, I set up a dedicated hostname: http://drop.hoopycat.com/.
Here's the flow:
- A request is received for
http://drop.hoopycat.com/filename. (Note: requests for/get redirected tohttp://hoopycat.com/). - If filename exists in
~rtucker/public_html/tmp, serve it up. - If it doesn't, issue a HTTP redirect to
http://dl.dropbox.com/u/3597105/filename.
Easy enough, and it works darn well. I do no checks for the file's existence on Dropbox, so their 404 page gets served up for totally bogus URLs. This is not a big deal in practice.
The configuration of lighttpd is simple, using mod_magnet and a small Lua script. To lighttpd.conf or a conf-available/ file of your choice, add:
Code:
server.modules += ( "mod_magnet" ) | |
| |
$HTTP["host"] == "drop.hoopycat.com" { | |
server.document-root = "/home/rtucker/public_html/tmp/" | |
url.redirect += ( "^/$" => "http://hoopycat.com/" ) | |
magnet.attract-physical-path-to = ( "/etc/lighttpd/conditional-redirect.lua" ) | |
} |
Then, create /etc/lighttpd/conditional-redirect.lua with the following script, borrowed from an example at http://redmine.lighttpd.net/projects/lighttpd/wiki/AbsoLUAtion:
Code:
local filename = lighty.env["physical.path"] | |
local stat = lighty.stat( filename ) | |
if not stat then | |
local static_name = string.match( filename, "tmp/([^/]+)$" ) | |
lighty.header["Location"] = "http://dl.dropbox.com/u/3597105/" .. static_name | |
return 302 | |
end |
Remember to replace the dl.dropbox.com URL with yours (and change everything else, too).
Assuming DNS is set up correctly and mod_magnet is installed (it's lighttpd-mod-magnet in Ubuntu 8.04), a restart of lighttpd should yield good results!
No feedback yet
Comments are closed for this post.