Search code examples
wildflyundertow

Advanced Tweak on Undertow-handlers.conf for http https redirect


I use WildFly behind an AWS load balancer. I want the Undertow server in WildFly to redirect http traffic to https, and I can do this mostly successfully with the following line placed in undertow-handlers.conf:

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

Thanks to these folks for getting me this far! Now here's my desired tweak. Sometimes I run my web application behind a testing load balancer using 'dev.server.com' and sometimes I run it behind a production load balancer using 'app.server.com.' Currently, I have to remember to manually edit undertow-handlers.conf any time I switch balancers. I'm hoping there is a way to change the hard-coded 'dev' and 'app' to something mechanical. Is there a way to tell Undertow to just use the domain name that was originally requested?

Thanks.


Solution

  • Thankfully the undertow configuration gives you access to the request headers via Exchange Attributes, which you're already using to access the X-Forwarded-Proto header. So the solution is to simply use the Host header from the request like so:

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