Found one interesting overlook configuration in nginx.

server {
  server_name ~^(www\.)?(?<domain>.+)$;

  location / {
    root /sites/$domain;
  }
}

Some note on the matching pattern modifiers,

(none) prefix match
=    match exactly
~    case sensetive regex match
~*   case insensetive regex match
^~   non regular match is prefered

Related
Nginx core module configuration
Understanding server and location block selection algorithm