【问题标题】:Redirecting wildcard subdomains to a different top-level domain with nginx使用 nginx 将通配符子域重定向到不同的顶级域
【发布时间】:2011-05-18 16:14:09
【问题描述】:

我们有一堆通配符子域(_foo.example.com、bar.example.com 等),当通过 HTTPS 访问时,它们应该重定向到我们安全域上的等效子域。

一些例子:

我认为这可以通过 nginx 重写来完成,但我不确定语法。这是我正在尝试的:

server {
    listen        443;
    server_name   *.example.com;

    rewrite       ^(.*)   https://*.secure.com$1 permanent;
}

这显然行不通,因为我没有捕获传入的子域并在重写中使用它。

【问题讨论】:

    标签: ssl redirect https nginx rewrite


    【解决方案1】:

    试试这样的东西(未经测试):

    server {
        listen 80;
        listen 443 default ssl;
    
        server_name "~^(?<name>\w\d+)\.example\.com$";
    
        rewrite ^(.*) https://$name.secure.com$1 permanent;
    }
    

    【讨论】:

    • 我喜欢这个解决方案,它对我有用(为我自己的目的进行了大量修改)。
    【解决方案2】:

    http://forum.slicehost.com/comments.php?DiscussionID=730找到这个

    # redirects arbitrary subdomain (some.random.sub.example.com) to (some.random.sub.example.org)
    if ($host ~* "^([^.]+(\.[^.]+)*)\.example.com$"){
      set $subd $1;
      rewrite ^(.*)$ http://$subd.example.org$1 permanent;
      break;
    }
    
    # Simply redirects example.com to example.org
    if ($host ~* "^example.com$"){
      rewrite ^(.*)$ http://example.org$1 permanent;
      break;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-13
      • 2017-12-03
      • 1970-01-01
      • 2016-03-28
      • 2016-03-27
      • 2010-11-30
      • 1970-01-01
      • 2014-05-29
      相关资源
      最近更新 更多