【问题标题】:How to exclude specific subdomains server_name in nginx configuration如何在 nginx 配置中排除特定的子域 server_name
【发布时间】:2016-05-04 09:55:09
【问题描述】:

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

我的配置如下

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

我不想更改发往xyz.example.com的任何请求

【问题讨论】:

    标签: nginx nginx-location


    【解决方案1】:

    '*' 通配符被 nginx 忽略:

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

    【讨论】:

      【解决方案2】:

      您至少需要两个服务器块,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/;
      }
      

      【讨论】:

      • 为什么这被标记为“正确答案”?这是错误的,不起作用!
      猜你喜欢
      • 2016-08-26
      • 2021-02-20
      • 2019-06-03
      • 1970-01-01
      • 2020-10-07
      • 2011-07-16
      • 2012-04-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多