如何在nginx配置中排除特定的子域server_name

时间:2016-01-27 12:33:16

标签: nginx nginx-location

我在server_name中使用通配符。我想将example.com(配置为* .example.com)的所有子域重定向到foo.comxyz.example.com

我的配置如下

server {
        listen          80;
        server_name     *.example.com;
        location / {
            proxy_pass      http://$1.foo.com;
        }
}

我不想更改xyz.example.com

的任何请求

2 个答案:

答案 0 :(得分:9)

您至少需要两个服务器块,nginx将选择更具体的服务器块来处理请求。有关详细信息,请参阅this document

您需要xyz.example.com的服务器块,例如:

server {
    listen      80;
    server_name xyz.example.com;

    location / {
        proxy_pass http://$1.foo.com;
    }
}

然后是default_server或通配符服务器,例如:

server {
    listen 80;
    server_name *.example.com;
    return http://foo.com/;
}

或者:

server {
    listen 80 default_server;
    return http://foo.com/;
}

答案 1 :(得分:0)

nginx忽略'*'通配符:

nginx:[警告] 0.0.0.0.:80上的服务器名称“ * .example.com”冲突,被忽略