Incorrect relative URLs when Tomcat is fronted by Apache?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Incorrect relative URLs when Tomcat is fronted by Apache?

Matt Jones
Hi,

I have an Apache web server in front of a Tomcat instance that hosts my Spring webapp.

I have a simple login form that looks like this:

	<form th:action="@{/login}" method="post">
		<label>Username:</label> <input name="username" /> <label>Password:</label>
		<input name="password" type="password" /> <input name="submit"
			type="submit" title="Login" />
	</form>

myapp.war is deployed to Tomcat with a context root of /myapp

When I hit the Tomcat instance directly, Thymeleaf resolves the URL @{/login} above correctly to /myapp/login, however when I go via the Apache web server, the URL is evaluated incorrectly as /login.

Does anybody know what I need to set to tell Thymeleaf to resolve this URL correctly?

Thank you,
Matt
Reply | Threaded
Open this post in threaded view
|

Re: Incorrect relative URLs when Tomcat is fronted by Apache?

danielfernandez
Administrator
Thymeleaf asks the Servlet API for the context path (which appears to be "myapp", in this case). Specifically, it does so by executing the "getContextPath()" of the HttpServletRequest object. Once generated, the URL is passed to the HttpServletResponse.encodeUrl() method in order to give an opportunity to URL-rewriting filters to adjust the generated URLs.

So if you observe a difference between how your context is computed with and without an HTTPd acting as proxy, it could be because you have some kind of URL Rewriting filter in place at your Tomcat reading X-Proxy HTTP headers coming from the proxy, or maybe because you have some kind of URL-matching-and-rewriting module in HTTPd rewriting the URLs in your HTML payload.

As for Thymeleaf, all these context-related computations are done by calling methods in the Servlet API itself, so any influence in the obtained values will come from external influences.
Reply | Threaded
Open this post in threaded view
|

Re: Incorrect relative URLs when Tomcat is fronted by Apache?

Matt Jones
Thanks Daniel, that's really helpful.

Matt