Nginx: Need a map function for domain of http_name

903 Views Asked by At

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;
}
1

There are 1 best solutions below

0
On

Thanks to Ivan Shatsky, the solution was:

map $host $this_domain {
        default $host;
        ~www1\.(.*)$ $1;
}