More subdomain mapping (dynamic 'root'?)

I want to be able to map submains and urls to determined locations on
disk, if they exist.

The URL structure is something like:

Where would letters, numbers and underscores , and is a domain name

Nginx should check if /var/www/clients//admin//html/ exists and use that
as root:

Some horrible pseudo code:

server {
listen localhost: 80;
server_name *;

if ($host ~ () {
set $client = $1;
if ($url ~ ^([^/]
)) {
set $project = $1;
set $myroot = /var/www/clients/$client/admin/$project/html;
if ( -e $myroot ){
root $myroot;


If this is not very ‘nginxy’, I am very open to alternate solutions.

Posted at Nginx Forum: