Arbitrarily slicing $server_name into subdomains?

Hi there, hope all are well!

I’d like to match a wildcard DNS name, pointing to an nginx virtual
host, such that an arbitrary level of subdomains maps to an arbitrary
level of directories, in a manner I specify.

For example:

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.)

It’s straightforward to do this for a fixed level of
subdomains/directories using a server_name regex, for example:

    server_name    ~^(.*)\.(.*)\.myserver\.net$;
    set    $my_host $2/$1;
    root    /var/www/docs/myserver.net/$my_host/public

which gives me project.client.myserver.net
…/myserver.net/client/project/… - but the whole point of using
wildcards like this is that I don’t want to keep adding new configs.

Is it possible to do this, perhaps using the perl config module? Or
maybe just a smarter regex?

Very grateful for any suggestions.

Thanks,
Igor C.

BTW, I just realised that example could even be

server_name    ~^(.*)\.(.*)\.myserver\.net$;
root    /var/www/docs/myserver.net/$2/$1/public;
  • and I’m on 0.7.64.

cheers
Igor C.

Igor C. 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

Thanks Tobia. That makes sense, though it makes the obsessive tidier
inside me a bit uncomfortable :wink:

It would be great to be able to do a real arbitrary list, I wonder if
there’s a way to do that?

cheers
Igor C.

Tobia C. wrote:

Igor C. 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

On Sat, Jan 9, 2010 at 1:03 PM, Igor C. [email protected] wrote:

It would be great to be able to do a real arbitrary list, I wonder if
there’s a way to do that?

Hint: In your application and just pass the full domain to it :wink:

– Merlin

On Mon, Jan 11, 2010 at 11:21 AM, merlin corey [email protected]
wrote:

On Sat, Jan 9, 2010 at 1:03 PM, Igor C. [email protected] wrote:

It would be great to be able to do a real arbitrary list, I wonder if
there’s a way to do that?

Hint: In your application and just pass the full domain to it :wink:

– Merlin

To clarify, “your application” is some mythical and simple application
that just does routing the way you want. 20 line or less python
script including the fastcgi and daemonize ;).

– Merlin

merlin corey wrote:

Hint: In your application and just pass the full domain to it :wink:

– Merlin

To clarify, “your application” is some mythical and simple application
that just does routing the way you want. 20 line or less python
script including the fastcgi and daemonize ;).

– Merlin

Thanks Merlin. Part of the point is to be able to run code unmodified in
various environments, inside dynamic vhosts using wildcard DNS. Do you
mean as a 3rd layer between nginx and the web app?

Cheers
Igor C.

On Thu, Jan 14, 2010 at 2:32 PM, Igor C. [email protected]
wrote:


nginx mailing list
[email protected]
nginx Info Page

I don’t personally like or support the idea of overcomplicating the
configuration for a false perception of simplicity…

The BEST solution, in my opinion, is to generate static server blocks
for each known domain and regenerate configuration as needed (when a
new document root is created, the new server is created too).

Still, for the arbitrary subdomains, instead of trying to shoe-horn it
into this unnecessary heirarchical organization you just do a simple
flat mapping of subdomains to directories, like so:

server {
listen 80;
server_name ~^(.*).mydomain.tld$;
root /var/www/mydomain.tld/$1/public;
}

If you do it in the manner you can support arbitrary length
subdomains. For example…

sub.mydomain.tld → /var/www/mydomain.tld/sub/public
a.b.mydomain.tld → /var/www/mydomain.tld/a.b/public
i.complicate.configs.mydomain.tld →
/var/www/mydomain.tld/i.complicate.configs/public

Essentially, there is no reason to think of the dot separator in
domain names as equivalent to a directory hierarchy other than that it
is comfortable.

– Merlin

Igor C. wrote:

Thanks Tobia. That makes sense, though it makes the obsessive tidier inside me a bit uncomfortable :wink: It would be great to be able to do a real arbitrary list, I wonder if there’s a way to do that?

What’s the problem? The multiple ///? They are normalized at the OS
level! Or maybe it’s the upper limit to the subdomains? Do you need more
than 9 subdomains?

merlin corey wrote:

a simple application that just does routing the way you want. 20 line or less python script including the fastcgi and daemonize

Sure. But I still like my regexp better. It does exactly what he needs,
it’s simpler (2 lines of code), fewer points of failure, and it’s surely
faster (libpcre, already compiled into nginx)

Tobia

Hi Merlin, thanks.

merlin corey wrote:

Essentially, there is no reason to think of the dot separator in
domain names as equivalent to a directory hierarchy other than that it
is comfortable.

Well, and that directory hierarchies allow me to share and mount
directories in ways which make graphic designers and front-end people
feel comfortable, and organise large numbers of sites in a sensible
hierarchy so that you don’t end up with large numbers of folders in one
place that people have to navigate through with GUI tools. It’s fine for
me to glob through dot hierarchies in a shell, but other people like to
work differently.

I’m not saying it’s the right way (or even that there is a right way!)
but it’s what I’d like to do, so if there’s a way to do it, it would
make me happy. :slight_smile:

Cheers
Igor C.

Hi Tobia,

Tobia C. wrote:

What’s the problem? The multiple ///? They are normalized at the OS
level! Or maybe it’s the upper limit to the subdomains? Do you need more
than 9 subdomains?

Not a major ‘problem’ as such, I just would like to be able to do it
without redundancy or limitations. I guess I’m being a bit OCD about it,
but that’s the way I am. :wink: If it’s not possible to do arbitrarily
deeply as I described, then your suggestion is a good one. Thanks.

Best
Igor C.