Com outro dia inteiro de googling, encontrei esta jóia:
####################### Magic RewriteRules #######################
uninitialized_variable_warn off;
##### Rewrite rules for domain.tld => www.domain.tld #####
if ($host ~* ^([^.]+\.[^.]+)$) {
set $host_without_www $1;
rewrite ^(.*) $scheme://www.$host_without_www$1 permanent;
}
##### Rewrite rules for subdomains with automatic SSL support #####
set $redirect_ssl 'no';
if ($host ~* ^(.*)\.([^.]+\.[^.]+)$) {
set $ssl_subdomain $1;
set $host_without_www $1.$2;
}
if (-e $document_root/config/ssl/$ssl_subdomain) {
set $redirect_ssl 'yes';
}
if ($scheme = 'https') {
set $redirect_ssl 'no';
}
if ($redirect_ssl = 'yes') {
rewrite ^(.*) https://$ssl_subdomain.$host_without_www$1 permanent;
}
##### Rewrite rules for automatic authentication #####
if ($host ~* ^([^.]+)\.[^.]+\.[^.]+$) {
set $auth_subdomain $1;
}
if (-e $document_root/config/auth/$auth_subdomain) {
rewrite ^(.*)$ /auth$1;
break;
}
##### Rewrite rules for automatic subdirectory rewriting #####
set $redirect_subdir 'yes';
if ($redirect_subdir_done = 'yes') {
set $redirect_subdir 'no';
}
if ($host ~* 'www\.[^.]+\.[^.]+$') {
set $redirect_subdir 'no';
}
if ($host ~* ^([^.]+)\.[^.]+\.[^.]+$) {
set $subdir_domain '$1';
}
if ($redirect_subdir = 'yes') {
set $redirect_subdir_done 'yes';
rewrite ^(.*)$ /$subdir_domain$1 break;
}
#################### End Of Magic RewriteRule ####################