使用nginx将通配符子域名重定向到不同的顶级域名

3
我们有一堆通配符子域名(_foo.example.com、bar.example.com等),当通过HTTPS访问时,应重定向到我们安全域上的相应子域名。
一些示例:
- https://foo.example.com => https://foo.secure.com - https://foo.example.com/some/path => https://bar.secure.com/some/path - https://bar.example.com => https://bar.secure.com 我认为这可以通过nginx重写来实现,但我不确定语法。这是我的尝试:
server {
    listen        443;
    server_name   *.example.com;

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

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

3
尝试像这样做(未经测试):

试试以下代码:

server {
    listen 80;
    listen 443 default ssl;

    server_name "~^(?<name>\w\d+)\.example\.com$";

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

我喜欢这个解决方案,它对我很有用(已经根据我的需求进行了大量修改)。 - pupeno

0
发现这个信息在 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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接