0

We would need the domain part of the requested host.

F.e.: If www1.domX.here is the requested host, we would need domX.here in variable.

Not working:

map $http_host $this_dom {
        default $http_host;
        ~^www1\.(?<this_dom>)$ $this_dom;
}

server {
        server_name 
                www1.dom1.here
                www1.dom2.here
                www1.dom3.here
                www1.dom4.here
                www1.dom5.here;

        location /somestuff {
                # local content delivery
                root /shared/somestuff;
        }

        location / {
                return 301 https://www.$this_dom$request_uri;
        }
}

This examle leads to an empty domain.

For the moment solved it this way:

if ($host ~* ^www1\.(.*)$) {
        set $this_dom $1;
}
Markus N.
  • 312
  • 2
  • 7
  • It is probably unwise to reuse the variable name `$this_dom`. Choose a different name for the named capture. – Richard Smith Oct 14 '21 at 12:22
  • Changing the variable remains the same, i do not like "if" but that solved the problem for the moment. – Markus N. Oct 15 '21 at 13:18
  • @MarkusN. Of course `(?)` named capture group will give you an empty `$this_dom` variable. Maybe you missed `.*` part meaning `(?.*)$`? Also you can check [this](https://serverfault.com/questions/1039781/is-there-a-variable-containing-the-domain-name-in-nginx) ServerFault thread. – Ivan Shatsky Oct 16 '21 at 08:59

1 Answers1

0

Thanks to Ivan Shatsky, the solution was:

map $host $this_domain {
        default $host;
        ~www1\.(.*)$ $1;
}
Markus N.
  • 312
  • 2
  • 7