使用nginx将通配符子域重定向到不同的顶级域名
问题描述:
我们有一堆通配符子域名(_foo.example.com,bar.example.com等),当通过HTTPS访问时应重定向到等效子域我们的安全域。使用nginx将通配符子域重定向到不同的顶级域名
一些例子:
- 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;
}
这显然不会工作,因为我没有捕获传入的子域,并在重写中使用它。
答
尝试这样的事情(未经测试):
server {
listen 80;
listen 443 default ssl;
server_name "~^(?<name>\w\d+)\.example\.com$";
rewrite ^(.*) https://$name.secure.com$1 permanent;
}
答
发现这对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;
}
我喜欢这个解决方案,它的工作对我来说(重大的修改,为我自己的目的)。 – Pablo 2012-12-03 15:51:42