Tomcat – How to ensure the Context Path is the same when accessing a web app via Apache (proxy pass – port 80) and tomcat (port 8080)

apache-2.2javajsptomcat

I have written a web app in Java/J2EE and am deploying it using apache (ec2 micro instance) to forward requests from port 80 to port 8080 (same machine). My httpd.conf looks like the following:

<VirtualHost *:80>
  ServerName localhost
  <Proxy *>
    Order deny,allow
    Allow from all
  </Proxy>
  ProxyPreserveHost On
  ProxyPass / http://localhost:8080/WEBAPP/
  ProxyPassReverse / http://localhost:8080/WEBAPP/
</VirtualHost>

The problem is that when I prepend <%=request.getContextPath()%> before my resources (urls, image path, js files, etc.) it prepends the same thing when I access my app from port 80 or 8080. Now when I access my app from port 80 all of my links are messed up and none of my JS, css, or urls work.

Also, I could be approaching setting up my web app entirely wrong so any advice to better set this up would be appreciated.

Asked this on stack overflow but was pointed here…

https://stackoverflow.com/questions/15736198/how-do-i-ensure-the-context-path-is-the-same-when-accessing-a-web-app-via-apache

EDIT

As mentioned in a comment below I am setting up a domain name and would like mydomain.com to load mydomain.com:8080/WEBAPP when I access mydomain.com. I'm not sure redirect is the correct answer because I don't want to just send the user to mydomain.com/WEBAPP (which is an easy fix) I want the URL to look nice and act intuitively, not just redirect the user everywhere. Also I'm trying to avoid using relative paths while access my resources (imgs, css, js, etc.) so that I can forward/redirect users from a servlet or access the jsp directly and the paths to be correct regardless of what is actually showing in the URL.

Hope that is a bit of a better explanation. Sorry again if I was not clear above.

Best Answer

You should fix your configuration:

<VirtualHost *:80>
  ServerName localhost
  <Proxy *>
    Order deny,allow
    Allow from all
  </Proxy>
  ProxyPreserveHost On
  ProxyPass /WEBAPP http://localhost:8080/WEBAPP/
  ProxyPassReverse /WEBAPP http://localhost:8080/WEBAPP/
</VirtualHost>

Additional option is

<VirtualHost *:80>
  ServerName localhost
  <Proxy *>
    Order deny,allow
    Allow from all
  </Proxy>
  ProxyPreserveHost On
  ProxyPass / http://localhost:8080/
  ProxyPassReverse / http://localhost:8080/
</VirtualHost>

Clarification: from the security point of view it is better to add redirection for each application (the first option). In this case all not appropriate requests will be stopped at Apache and will not reach Tomcat