【发布时间】:2016-06-13 19:34:30
【问题描述】:
我目前正在使用 nginx 并尝试重定向所有流量,例如firstdomain.org 到 seconddomain.org 使用简单的重定向可以正常工作,但我现在还希望它能够处理 URI、方案和子域。
例如
http(s)://firstdomain.org/ 重定向到http(s)://seconddomain.org/,
http(s)://firstdomain.org/test 重定向到 http(s)://seconddomain.org/test,
http(s)://test.firstdomain.org/ 重定向到 http(s)://test.seconddomain.org/
等等..
我目前的设置是这样的:
server {
listen 80;
listen 443 ssl;
server_name ~^(?<sub>\w+)\.firstdomain\.org$, firstdomain.org;
ssl_certificate /path/to/certificate;
ssl_certificate_key /path/to/certificatekety;
location / {
if ($sub = '') {
return 301 $scheme://seconddomain.org$request_uri;
}
return 301 $scheme://$sub.seconddomain.org$request_uri;
}
}
这是重定向没有子域的链接就好了,但只要它是例如http(s)://test.subdomain.org 或 http(s)://test.subdomain.org/test 它不再起作用了。
我有什么遗漏的吗,或者 nginx 是否支持更简单的方法来实现我想要做的事情?
【问题讨论】:
标签: redirect nginx webserver url-redirection http-redirect