views:

36

answers:

1

Recently I had to install a Java application for a client using Tomcat6. The application needed to run from the root of their domain so I also installed apache2 and mod_proxy_ajp to set up a proxy to make this work. After a bit of massaging and googling to deal with Location Headers including the original path of the servlets rather than the proxy root. I've come up with this.

<VirtualHost *:80>
        ServerName myclientssite.com
        ErrorLog /var/log/apache2/ajp.error.log
        CustomLog /var/log/apache2/ajp.log combined

        <Proxy *>
                AddDefaultCharset Off
                Order deny,allow
                Allow from all
        </Proxy>

        ProxyRequests Off
        ProxyPass / ajp://localhost:8009/appname/
        ProxyPassReverse / http://localhost:8080/appname/
        ProxyPassReverseCookiePath /appname/ /

        Header edit Location ^([^/]*//[^/]*)?/appname/(.*)$ $1/$2
</VirtualHost>

My question is wither this is the the best solution. It seems with out mod_headers and the Header edit line and headers will usually include the appname subdirectory.

A: 

Is the Java application building its Location headers from the information in the request (rather than from some explicit configuration)? In this case, it should get the location right if you use ProxyPreserveHost On.

The case where you'd need the to change the header is if your Apache Httpd frontend is over HTTPS and the connection from Apache Httpd to the Java container is over plain HTTP or AJP. Not that, in theory (and that might not always be the case in practice), the Location header requires an absolute URI, so you might not need such a complex expression if you already know the hostname.

I'd also suggest using an HTTP reverse proxy rather than AJP (it's quite similar in the end, but it seems to have more support).

ProxyRequests Off
ProxyPass /appname/ http://localhost:8080/appname/
ProxyPassReverse /appname/ http://localhost:8080/appname/
ProxyPreserveHost On

# If you're using HTTPS
Header edit Location ^http://www.example/appname/ https://www.example/appname/
Bruno
@Bruno I attempted to work with your solution and while try to implement it I noticed you're maintaining the "appname" directory on the apache side of the proxy. The main point of my implementation above was to remove "appname" from the users URL's so rather than "http://www.example.com/appname/*" the user would only be shown "http://www.example.com/*"I'm not able to get this implementation working correctly for Header Location redirects.
AWinter
I'm not sure if you can always redirect to a webapp that's not at the same prefix (it may depend on the way the app produces its relative link for referred content: it's not just about `Location`, but it's also about all the links the pages contain).You may have to deploy your app into your container's root to do this (I think in Tomcat, it's `ROOT.war` or `ROOT/`).If you don't have a strict need for load balancing or other things on you Apache Httpd, it might be worth removing it from the equation and having the requests come to the webapp container directly.
Bruno
If the app servlet is forced to run from a subdirectory under normal circumstances, is there a reasonable way to setup a context path or virtual host that makes it appear to run from root, from the users perspective?
AWinter
I don't think you can in general, since you need the container to be aware of the correct context path. As I said, it's not just about the Location header, it's also about all the links within the page, which may not be relative. You might need to rewrite the HTML too (See this: http://confluence.atlassian.com/display/DOC/Using+Apache+with+mod_proxy#UsingApachewithmod_proxy-SettheURLforredirection )Any reason why you can't deploy your webapp in your Tomcat root?
Bruno
The app it self is already deployed in root, but it unfortunately requires the use of subdirectories to determine what data it displays.
AWinter
Do you mean you have a http://localhost:8080/ for which you only even want to expose http://localhost:8080/someprefix/ via http://www.example/ directly? Why use some prefix then? A single webapp for multiple Apache Httpd VirtualHosts perhaps?I doubt that's possible. It might be worth asking on serverfault.com or on the Apache Tomcat user list.
Bruno
Thanks for the information. I'll ask on serverfault and in a few additional places.
AWinter