1

Been buggin' me.

So, I'm using JSP and there are these methods (simplistic example) inside my implemented Filter and my extended HttpServlet, respectively:

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    ((HttpServletResponse) response).sendRedirect(((HttpServletRequest) request).getContextPath() + "/foo");
}

 

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    response.sendRedirect(request.getContextPath() + "/foo");
}

At localhost:8080/app, both Filter and HttpServlet correctly redirect me to localhost:8080/app/foo.

But at www.mysite.com/app, which hides a tomcatserver:8080/app under Apache's proxy as follows,

RedirectPermanent /app /app/
ProxyPass /app/ http://tomcatserver:8080/app/
ProxyPassReverse /app/ http://tomcatserver:8080/app/

the Filter redirects me ok to www.mysite.com/app/foo, while the HttpServlet either:

  1. (from same domain) reveals the Tomcat's server address, redirecting me to tomcatserver:8080/app/foo or
  2. (from outside the domain) just gets stuck loading.

So... what's causing this?

P.S.: I know that removing the request.getContextPath() and the "/" part from the HttpServlet solves the problem, I'm not asking that.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555

1 Answers1

3

Because Tomcat isn't aware of the host name of the reverse proxy. The host header Tomcat sees is for itself. Therefore that is the host Tomcat uses when it generates absolute URLs for redirects etc.

You can change this by using

ProxyPreserveHost On

in your httpd config.

Mark Thomas
  • 16,339
  • 1
  • 39
  • 60