首页 > 解决方案 > Nginx:需要 http_name 域的映射函数

问题描述

我们需要请求主机的域部分。

Fe:如果www1.domX.here是请求的主机,我们需要domX.here变量。

不工作:

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

此示例导致一个空域。

目前以这种方式解决了它:

if ($host ~* ^www1\.(.*)$) {
        set $this_dom $1;
}

标签: nginxnginx-confighttp-host

解决方案


感谢Ivan Shatsky,解决方案是:

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

推荐阅读