Archive for January 20th, 2010
20
HOWTO: redirect iPhone/iPod users on nginx
0 Comments | Posted by id in howto, mobile, webdev
I learned a little something about nginx, a small footprint web server that is ideal for serving up mobile sites, or sites where you don’t want the heavy usage of Apache. Today, I needed to solve a problem where we redirected iPhone/iPod users to a different URL. Since nginx doesn’t use the old-style mod_rewrite rules, I had to learn how to enable redirection in the server.
Since nginx was already compiled with redirection support, I simply had to locate the correct configuration file and add a few lines of code, and away it went.
First, I checked out /etc/nginx/ and opened the site configuration file within the /sites-enabled/ path. For this example, let’s say the site was m.iandouglas.com:
# vi /etc/conf/nginx/sites-enabled/m.iandouglas.com
In here, I’d look for the ‘server’ block and add my redirection rules:
server {
listen 80;
server_name m.iandouglas.com;
root /var/www/m.iandouglas.com/public;
# redirect iPhone/iPod users to the new iphone site
if ($http_user_agent ~* '(iPhone|iPod)') {
rewrite ^/$ http://m.iandouglas.com/iphone/index.html;
}
.
.
.
Then a simple nginx restart:
# /etc/init.d/nginx restart
… and we were all set.
I think I was 8 years old when my dad came home with a Commodore 64 and various games. Hacking up those games in C64 Basic is what got me interested in programming, and now 20-something years later I landed a job at Armor Games as a Sr Web Developer. Not doing game programming, but I *am* in the industry where I’m happiest, and the web development work so far has been a nice change from the heavy lifting I’ve been doing in Perl for so long. For obvious reasons I won’t blog about specifics going on here at the office, but I may share insights into some of the things I’m learning.

