Serving Conditional Redirects with nginx
I discontinue a lot of shit. I'm pretty bad at that, but I tend to keep things like embeddable images and JavaScript in tact so I don't break the websites that rely on them. I often switch URLs around, hence the need for redirecting.
I was using my NearlyFreeSpeech.NET account to handle all of my redirecting, but I was serving about 12MB of redirect data a month; a bit of a waste of money when I could employ my Linode to deal with it.
So, nginx. Love it. It's not to complicated to set up, nor is it feature lacking. I use the http Host header to decipher where traffic should be sent by use of an if statement on a catch-all server, example config (for inside the http block) below:
server {
listen 80 default;
server_name _;
if ( $HOST = 'aelabs.net' ) {
rewrite ^/(.*)$ http://www.aelabs.net/$1 permanent;
}
if ( $HOST = 'alanedwardes.com' ) {
rewrite ^/(.*)$ http://www.alanedwardes.com/$1 permanent;
}
if ( $HOST = 'posts.alanedwardes.com' ) {
rewrite ^/(.*)$ http://www.alanedwardes.com/posts/$1 permanent;
}
}
As you can see, it's extremely simple to serve a redirect, and this is just written in the nginx config language. I'm not touching any web-specific languages like PHP, which would incur a longer response turnaround and higher resource usage per request. (Note that I have to redirect the "www" for my domain because the Google App Engine doesn't like naked domains, this isn't some crazy vendetta I've got against "naked" domain use.)
So, if you visit linode.alanedwardes.com, you get a 404, while alanedwardes.com sends a 301, and you get gracefully dumped (no oxymoron intended) at the www-ed up counterpart.
I started using nginx on my Linode on the 4th of July, and at the time of writing (10th of July) have served 35,124 redirects, and because I'm using nginx it hasn't impacted RAM or CPU at all.

