Nginx

nginx 重定向到另一個域,不要忘記子域

  • September 14, 2016

這是一個相當快的:我如何重定向到另一個域並仍然轉發子域?範例: http: //foo.domainone.com/bar.php -> http://foo.domaintwo.com/bar.php

提前致謝!

以下內容應該對您有用(這會將 test.com 重定向到 abc.com):

server {
# Listen on ipv4 and ipv6 for requests
listen 80;
listen [::]:80;

# Listen for requests on all subdomains and the naked domain
server_name test.com *.test.com;

# Check if this is a subdomain request rather than on the naked domain
if ($http_host ~ (.*)\.test\.com) {
   # Yank the subdomain from the regex match above
   set $subdomain $1;

   # Handle the subdomain redirect
   rewrite ^ http://$subdomain.abc.com$request_uri permanent;
   break;
}
# Handle the naked domain redirect
rewrite ^ http://abc.com$request_uri permanent;
}

這應該確保裸域和任何子(或子、子)域被重定向到新的“基本”域。實踐中的幾個例子:

phoenix:~ damian$ curl -I -H "Host: test.com" web1-france
HTTP/1.1 301 Moved Permanently
Server: nginx/1.1.1
Date: Sat, 08 Oct 2011 06:43:45 GMT
Content-Type: text/html
Content-Length: 160
Connection: keep-alive
Location: http://abc.com/

phoenix:~ damian$ curl -I -H "Host: subdomain1.test.com" web1-france
HTTP/1.1 301 Moved Permanently
Server: nginx/1.1.1
Date: Sat, 08 Oct 2011 06:43:50 GMT
Content-Type: text/html
Content-Length: 160
Connection: keep-alive
Location: http://subdomain1.abc.com/

phoenix:~ damian$ curl -I -H "Host: wibble.subdomain1.test.com" web1-france
HTTP/1.1 301 Moved Permanently
Server: nginx/1.1.1
Date: Sat, 08 Oct 2011 06:43:55 GMT
Content-Type: text/html
Content-Length: 160
Connection: keep-alive
Location: http://wibble.subdomain1.abc.com/

在重寫行上,您可以指定“最後”而不是“永久”來獲得 302 臨時移動而不是 301 永久移動。如果您要移動域,那麼您應該這樣做:)

希望這可以幫助。

引用自:https://serverfault.com/questions/319627