Nginx: Forward all Subdomains

Your original configuration is not taking advantage of the nginx configuration. With a rewrite like that nginx will have to do extensive parsing on each request. If you are in an environment where performance and quick response time is essential then you'll want to use server blocks.

# Add www and redirect subdomains.
server {  
    listen      80;
    server_name domain.com *.domain.com;
    rewrite     ^ http://www.domain.com$request_uri permanent;
}

This way there is no complex parsing, Nginx uses a hash table for the server lookups and the rewrite uses the already parsed $request_uri variable.