Wildcard Subdomains in nginx

Posted on June 14, 2008

The mission: redirect a page like this (note the www in URL) to this (look ma, no www!).

I’m using nginx’s handy vhosts feature. This is the pertinent part of the new config file:

server_name  .incredimazing.com;

# 301 redirect 'www.incredimazing.com' => 'incredimazing.com'
#  Also works with:  'www.incredimazing.com/Foo_Bar' => 'incredimazing.com/Foo_Bar'
if ($host = 'www.incredimazing.com') {
  rewrite ^/(.*)$ http://incredimazing.com/$1 permanent;
}

By setting the server name to ’.incredimazing.com’ this makes it so that ‘foo.incredimazing.com’, etc. will also get served by the same mongrels.

Handy if you give users their own subdomain, e.g. alexis.incredimazing.com

Comments
  1. Jim JeffersJune 22, 2008 @ 11:36 AM

    Hey Shanti,

    What’s the best way of handling the artificial subdomains in your rails app? I found this but it appears to be dated:

    http://agilewebdevelopment.com/plugins/request_routing