从 example.com/ 永久重定向到 https://www.example.com/dir/
Permanent redirect from example.com/ to https://www.example.com/dir/
我有一些基于微服务理念构建的软件。其中一个 - 托管在 example.com/
上变得毫无用处,所以我希望将用户重定向到托管另一个服务的 example.com/dir/
。 Whosebug
上最流行的解决方案是在配置文件中使用如下代码:
location = / {
return 301 $scheme://$http_host/dir/;
}
或
location = / {
return 301 https://$http_host/dir/;
}
它们都在我的服务器上失败了 - 服务器 returns 503 - to many computations
。堆栈跟踪显示,不存在无限规则,因此 load-balancer
.
可能会返回此代码
这个问题还有其他有效的解决方案吗? Whosebug
和 ServerFault
我最近 3 天仔细搜索过,任何解决方案都有效。
= 符号无效。
location / {
return 301 https://$http_host/dir/;
}
但这将以重定向循环结束,所以更具体一点:
location ~ ^[/]?$ {
return 301 https://$http_host/dir/;
}
在和我的队友一起研究后发现,不仅 return 301 url;
成功重定向了用户。尽管文档说避免使用 rewrite
但有效的是:
server {
...
rewrite ^/$ https://$http_host/dir/ permantent;
...
}
permantent
语句负责充当常规重定向。
重要:
在 nginx 中,^
是正则表达式和传入字符串的开头,$
符号是一个普通的 regex
符号,代表 theEndOfString
。所以正则表达式 ^/$
表示匹配 iff
url 等于 /
.
学分:
希望这会节省周末时间和金钱。
我有一些基于微服务理念构建的软件。其中一个 - 托管在 example.com/
上变得毫无用处,所以我希望将用户重定向到托管另一个服务的 example.com/dir/
。 Whosebug
上最流行的解决方案是在配置文件中使用如下代码:
location = / {
return 301 $scheme://$http_host/dir/;
}
或
location = / {
return 301 https://$http_host/dir/;
}
它们都在我的服务器上失败了 - 服务器 returns 503 - to many computations
。堆栈跟踪显示,不存在无限规则,因此 load-balancer
.
这个问题还有其他有效的解决方案吗? Whosebug
和 ServerFault
我最近 3 天仔细搜索过,任何解决方案都有效。
= 符号无效。
location / {
return 301 https://$http_host/dir/;
}
但这将以重定向循环结束,所以更具体一点:
location ~ ^[/]?$ {
return 301 https://$http_host/dir/;
}
在和我的队友一起研究后发现,不仅 return 301 url;
成功重定向了用户。尽管文档说避免使用 rewrite
但有效的是:
server {
...
rewrite ^/$ https://$http_host/dir/ permantent;
...
}
permantent
语句负责充当常规重定向。
重要:
在 nginx 中,^
是正则表达式和传入字符串的开头,$
符号是一个普通的 regex
符号,代表 theEndOfString
。所以正则表达式 ^/$
表示匹配 iff
url 等于 /
.
学分:
希望这会节省周末时间和金钱。