使用 nginx 重定向特定域的所有流量
redirect all traffic for a certain domain using nginx
我目前正在使用 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 支持的更简单的方法来实现我想做的事情?
您可以通过捕获 $sub
中的 .
来简化:
server {
listen 80;
listen 443 ssl;
server_name ~^(?<sub>\w+\.)?firstdomain\.org$;
ssl_certificate /path/to/certificate;
ssl_certificate_key /path/to/certificatekety;
return 301 "$scheme://${sub}seconddomain.org$request_uri";
}
我目前正在使用 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 支持的更简单的方法来实现我想做的事情?
您可以通过捕获 $sub
中的 .
来简化:
server {
listen 80;
listen 443 ssl;
server_name ~^(?<sub>\w+\.)?firstdomain\.org$;
ssl_certificate /path/to/certificate;
ssl_certificate_key /path/to/certificatekety;
return 301 "$scheme://${sub}seconddomain.org$request_uri";
}