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.

Posted in howtomobilewebdev.

Tagged with .