[英]redirect all traffic for a certain domain using nginx
I'm currently playing around with nginx and am trying to redirect all traffic for eg firstdomain.org to seconddomain.org This is working fine with a simple redirect but I now also want it to hand on the URI, the scheme and the subdomain. 我目前正在玩Nginx,并尝试将所有流量(例如firstdomain.org)重定向到seconddomain.org。通过简单的重定向可以正常工作,但现在我还希望它可以处理URI,方案和子域。
Eg 例如
http(s)://firstdomain.org/
redirects to http(s)://seconddomain.org/
, http(s)://firstdomain.org/test
redirects to http(s)://seconddomain.org/test
, http(s)://test.firstdomain.org/
redirects to http(s)://test.seconddomain.org/
and so on.. 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/
http(s)://test.firstdomain.org/
http(s)://test.seconddomain.org/
,依此类推。
My current set up is like this: 我当前的设置是这样的:
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;
}
}
This is redirecting links without subdomain just fine but as soon as it's eg http(s)://test.subdomain.org
or http(s)://test.subdomain.org/test
it does not work anymore. 这是在没有子域的情况下重定向链接就可以了,但是例如在
http(s)://test.subdomain.org
或http(s)://test.subdomain.org/test
它就不再起作用了。
Is there anything I have missed or is there maybe even an easier way nginx supports to achieve what I want to do? 是否有我想念的东西,或者甚至还有更简单的方法,nginx支持实现我想做的事情?
You can simplify by capturing the .
您可以通过捕获来简化
.
in $sub
: 在
$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";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.