Reputation: 4670
I have this complex Java application which is hosted behind a reverse-proxy.
What is the best practice to determine your user-facing url at the java application level when calling request.getServerName(), request.getServerPort() and friends ?
We are using Tomcat (but we might switch to an embedded jetty) behind an Apache mod_proxy (but we'll definitely switch to Amazon Elastic Load Balancer). I have listed 4 solutions:
Host
HTTP header of the request ServletRequest
API. Instead have the full qualified name of the server in a config file and read this config from our code.Our current solution :
I definitely need to stop using approach 1 and I would like to settle on one of the other three propositions.
EDIT: This can be summarized as :
request.getServerName()
?Host
HTTP header ?Upvotes: 4
Views: 867
Reputation: 173
you can trust the HOST header passed on by the mod_proxy on Tomcat if you configure tomcat to preserver the HOST from the request i.e. using Directive:
ProxyPreserveHost On
Upvotes: 1