由於某些原因,www.server-1.com/forum 不會重新導向至 forum.server-1.com,而是重新導向至 www.server-2.com。當我刪除將www.server-1.com 重定向到www.server-2.com 的部分時,它就像一個魅力......好吧,不完全是,因為每當論壇get 之後的第一個斜線都會在過程中遺失。
# Redirect www.server-1.com/forum to forum.server-1.com
# Redirect www.server-1.com/forum/ to forum.server-1.com
# Redirect www.server-1.com/forums to forum.server-1.com
# Redirect www.server-1.com/forums/ to forum.server-1.com
# Redirect www.server-1.com to www.server-2.com
server {
listen 80;
server_name www.server-1.com;
location = /forum {
rewrite ^ http://forum.server-1.com redirect;
break;
}
location /forum/ {
rewrite ^/forum/(.*) http://forum.server-1.com$1 redirect;
break;
}
location = /forums {
rewrite ^ http://forum.server-1.com redirect;
break;
}
location /forums/ {
rewrite ^/forums/(.*) http://forum.server-1.com$1 redirect;
break;
}
rewrite ^ http://www.server-2.com redirect;
}
# Redirect forum.server-1.com to www.server-3.com/forum (where the forum is hosted)
server {
listen 80;
server_name forum.server-1.com;
rewrite ^(.*) http://www.server-3.com/forum$1 redirect;
}
# Redirect forums.server-1.com to forum.server-1.com
server {
listen 80;
server_name forums.server-1.com;
rewrite ^(.*) http://forum.server-1.com$1 redirect;
}
# Redirect server-1.com and *server-1.com to www.server-1.com
server {
listen 80;
server_name server-1.com *.server-1.com;
rewrite ^(.*) http://www.server-1.com$1 redirect;
}
答案1
nginx 不一定會依照指令在設定檔中出現的順序處理指令。伺服器級重寫指令將在位置選擇之前處理,這解釋了您所看到的行為。由於伺服器中的所有內容都只是重寫,因此您應該能夠使用它:
server {
server_name www.server-1.com;
rewrite ^/forums?(.*) http://forum.server-1.com$1 redirect;
return 302 http://www.server-2.com/;
}