tomcatmod-proxy

Tomcat server.xml not working for multiple hosts


I'm forwarding requests from Apache to Tomcat.

<Server>
<Service>
    <Connector port="8222" protocol="HTTP/1.1" proxyPort="80" />

   <Engine name="Catalina" defaultHost="www.AAA.com">
            <Host name="www.AAA.com">
                 <Context path="/path" docBase="aaa"></Context>
            </Host>

            <Host name="www.BBB.com">
                 <Context path="/path" docBase="bbb"></Context>
            </Host>
   </Engine>
</Service>
</Server>

Requests via Apache - work only for the domain set in Engine element's defaultHost attribute.

If I set defaultHost to domain BBB then the second host urls work.

It's almost as if Tomcat is not receiving the original domain name and cannot figure out the target domain and therefore using the default.

I'm forwarding the requests via Apache mod_proxy and requests are being received depending on how I set defaultHost.

This is my proxy command

ProxyPass         /path http://localhost:8222/path
ProxyPassReverse  /path http://localhost:8222/path

I checked the access logs and requests (even non working ones) are reaching Tomcat.

I checked the x-forwarded-host and x-forwarded-server headers received by Tomcat and they're correct with 'www' and without respectively...along with the used domain name.

I would think Tomcat should be able to find the right Host with that info.


Solution

  • What you are looking for is the ProxyPreserveHost directive, which uses the Host header of the original request in the requests to proxied servers:

    ProxyPreserveHost On
    

    With the default configuration, the Host header is set to whatever is written in the ProxyPass directive. In your case it's localhost and since Tomcat does not have a Host of that name, the request is forwarded to the default host.

    Remark: in your case it would be easier to use mod_proxy_ajp and open an AJP connector on Tomcat. Using AJP Tomcat will receive (by default) all the information (IP address, hostname, server port, protocol used, etc.) of the original request, whereas with HTTP you'll need something like RemoteIpValve for Tomcat to retrieve that information.