NGINX - Performance Tuning

Naked Domain Redirect

On most situations, one can use this code to redirect to the www sub-domain:

server {
    ...
    if ( $host !~* ^www\. )
    {
        rewrite ^(.*)$ http://www.domain.com$1;
    }
}

This has performance issues since NGINX has to evaluate the IF block for every request. Instead, you can use server blocks perform a HTTP 301 redirect:

server {
    listen 123.123.123.123;
    server_name domain.com;
    return 301 http://www.domain.com$request_uri;
}

server {
    listen 123.123.123.123;
    server_name www.domain.com;
    ...

}