It looks like this might be working for me.
http {
server {
listen 80;
index index.html index.htm index.php;
set $basepath "/var/www/vhosts";
server_name ~^(?<sub>.+?)\.(?<domain>.+)$;
set $root $basepath/$domain/htdocs/www;
# if the domain is missing a subdomain then the default path will fail
if ( !-d $basepath/$domain/htdocs/www ){
rewrite ^ $scheme://www.$host$uri redirect;
}
set $root $basepath/$domain/htdocs/$sub;
# sub domain path doesn't exist redirec to default
if ( !-d $root ){
rewrite ^ $scheme://www.$domain$uri redirect;
}
location / {
root $root;
}
location ~ \.php$ {
root $root;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
}
http {
server {
listen 80;
index index.html index.htm index.php;
set $basepath "/var/www/vhosts";
server_name ~^(?<sub>.+?)\.(?<domain>.+)$;
set $root $basepath/$domain/htdocs/www;
# if the domain is missing a subdomain then the default path will fail
if ( !-d $basepath/$domain/htdocs/www ){
rewrite ^ $scheme://www.$host$uri redirect;
}
set $root $basepath/$domain/htdocs/$sub;
# sub domain path doesn't exist redirec to default
if ( !-d $root ){
rewrite ^ $scheme://www.$domain$uri redirect;
}
location / {
root $root;
}
location ~ \.php$ {
root $root;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
}