It's a bit of a hack, probably better done with map, but it does the job. Let it inspire you to make it better :)
____server {
______listen 80;
______server_name .mydom.com;
______access_log logs/mydom.com.access.log main;
______error_log logs/mydom.com.error.log;
______if ($host ~ "^(w{3}\.)?(.*?)\.?mydom\.com$") { set $myuri $2; }
______if ($myuri = '') { set $myuri 'www'; }
______set $myroot '/dynhosts/$myuri';
______if ($myuri = 'www') { set $myroot '/dynhostsroot/'; }
______root $myroot;
______location / {
________index index.html index.htm;
________try_files $myroot $myroot/ =404;
______}
____}