由于某种原因,www.server-1.com/forum 不会重定向到 forum.server-1.com,而是 www.server-2.com。当我删除将 www.server-1.com 重定向到 www.server-2.com 的部分时,它运行正常……嗯,不完全是,因为只要 URL 不是以 /forum 结尾,而是以 /forum/index.php 结尾,forum 后面的第一个斜线就会在过程中丢失。
# 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/;
}