用于http https重定向的Undertow-handlers.conf的高级调整

问题描述 投票:2回答:2

我在AWS负载均衡器后面使用WildFly。我希望WildFly中的Undertow服务器将http流量重定向到https,我可以通过在undertow-handlers.conf中放置以下行来成功完成此操作:

equals('http',%{i,X-Forwarded-Proto}) - >重定向(https://app.server.com%U

感谢these folks让我走到这一步!现在这是我想要的调整。有时我使用'dev.server.com'在测试负载均衡器后面运行我的Web应用程序,有时我使用'app.server.com'在生产负载均衡器后面运行它。目前,我必须记得在任何时候切换平衡器时手动编辑underow-handlers.conf。我希望有一种方法可以将硬编码的'dev'和'app'改为机械的东西。有没有办法告诉Undertow只使用最初请求的域名?

谢谢。

wildfly undertow
2个回答
3
投票

值得庆幸的是,underow配置允许您通过Exchange Attributes访问请求标头,您已经使用它来访问X-Forwarded-Proto标头。因此解决方案是简单地使用请求中的Host头,如下所示:

equals('http', %{i,X-Forwarded-Proto}) -> redirect(https://%{i,Host}%U)


3
投票

如果要将其作为部署的一部分,请尝试在重定向表达式中使用%h。例如:

equals('http', %{i,X-Forwarded-Proto}) -> redirect(https://%h%U)

另一种选择是配置服务器以便为您处理重定向。 CLI命令看起来类似于以下内容,假设http的默认端口为8080,https的默认端口为8443。

/subsystem=undertow/configuration=filter/rewrite=http-to-https:add(redirect=true, target="https://%h:8443%U")
/subsystem=undertow/server=default-server/host=default-host/filter-ref=http-to-https:add(predicate="equals(%p, 8080)")

您可以在Undertow documentation中查看所有可能的交换属性。

© www.soinside.com 2019 - 2024. All rights reserved.