0

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.

  • You can also promote my answer :) – Michael Apr 2 '13 at 5:00
0

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

| improve this answer | |
  • Sorry, I'll edit the original post. I want servername:80 to redirect to servername:8080/WEBAPP. What I'm doing is setting up a domain name and want mydomain.com to redirect to mydomain.com:8080/WEBAPP... which it does. Just the context path is messed up now because it is still grabbing /WEBAPP and not just / – user150878 Apr 1 '13 at 16:13
  • It is not correct to redirect from servername:80 to servername:8080/WEBAPP. You need to redirect from servername:80/WEBAPP to servername:8080/WEBAPP. You need to add redirection to each new WebApp (For example servername:80/WEBAPP1 to servername:8080/WEBAPP1). I recommend to use my configuration. – Michael Apr 1 '13 at 16:19
  • I'm not even saying ProxyPass/ContextPath is the correct solution to this problem but I definitely don't want the name of the web app in the URL for port 80. I also want my resources on each JSP to work correctly regardless of how I load the page (jsp, servlet forward/redirect, or from either port). – user150878 Apr 1 '13 at 16:25
  • In this case you need redirect from servername:80/ to servername:8080/ Your configuration creates the confusion with the context – Michael Apr 1 '13 at 16:31
  • Fair enough. So should I set up tomcat to use / (instead of /WEBAPP) to access the app? – user150878 Apr 1 '13 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.