Arbitrarily slicing $server_name into subdomains?

Tobia Conforto tobia.conforto at gmail.com
Fri Jan 8 18:28:46 MSK 2010


Igor Clark wrote:
>   url: http://client.myserver.net/
>   root: /var/www/docs/myserver.net/client/public
> 
>   url: http://project.client.myserver.net/
>   root: /var/www/docs/myserver.net/client/project/public
> 
>   url: http://sub.project.client.myserver.net/
>   root: /var/www/docs/myserver.net/client/project/sub/public
> (etc.)

In this case, a smarter regexp is enough:

  server_name ~ ^([^.]+\.)?([^.]+\.)?([^.]+\.)?([^.]+\.)?myserver\.net$;
  root /var/www/docs/myserver.net/$4/$3/$2/$1/public;

Add as many ([^.]+\.)? blocks as you need. This works because /var/www/docs/myserver.net/////public is a valid UNIX path, equivalent to /var/www/docs/myserver.net/public

-Tobia


More information about the nginx mailing list