Tomcat – How to get port redirects for two Servers running on same server

apache-2.2portredirecttomcat

I have an Amazon cloud running which is hosting two tomcat instances (ports 8080 and 8081) with each tomcat running a web application (foo and bar) for different customers.

However I am having a problem trying to get the port redirect working correctly.

I want users who come to my website at www.foo.com to be redirected automagically
to www.foo.com:8080/foo_app and users who come to my website at www.bar.com to be redirected automagically to www.bar.com:8180/bar_app.

But, I also want the port numbers to NOT be part of the URL. This is vitally important
since most of the users are in environments where they cannot navigate to www.foo.com:8080 or www.bar.com:8180 and can only communicate on port 80.

So anyone who types in www.bar.com/bar_app should be redirected to www.bar.com:8081/bar_app but the URL still must be www.bar.com/bar_app

The way I have attempted to achieve this is to do the following:

I used the following command to map any communication on port 80 to port 8080

sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080

This is great for the foo users but the bar users are also getting re-directed. Is it
possible to make this iptable mapping only applicable for certain "servers" ?

I have also added entries into /etc/apache2/httpd.conf

to allow mapping from my server to the actual webapp

<VirtualHost *:80>
    ProxyPreserveHost On
    ProxyPass / http://123.123.123.123:8080/foo_app
    ProxyPassReverse / http://176.34.212.135:8080/foo_app
    ServerName www.foo.com
    ServerAlias www.foo.com
</VirtualHost>

<VirtualHost *:80>
    ProxyPreserveHost On
    ProxyPass / http://123.123.123.123:8180/bar_app
    ProxyPassReverse / http://176.34.212.135:8180/bar_app
    ServerName www.bar.com
    ServerAlias www.bar.com
</VirtualHost>

I would greatly appreciate help from anyone who can advise on an approach to take.

Thank you

Best Answer

You don't need the iptables configuration. In fact, that's probably where your issue comes from. Apache will do the proxying. Apache receives the requests on port 80 for either domain name and proxies the requests to the right Tomcat server on port 8080 or 8180 like you have.

What you have is generally fine. Make sure you have the mods enabled (proxy, proxy_http).

a2enmod proxy
a2enmod proxy_http

Restart Apache.

These may be already enabled - it will tell you if they were.

Here is what I suggest for your VHost entries:

<VirtualHost *:80>
    ServerName www.foo.com

    ProxyPreserveHost On

    ProxyPass /foo_app http://123.123.123.123:8080/foo_app
    ProxyPassReverse /foo_app http://176.34.212.135:8080/foo_app

    RewriteEngine on

    RewriteRule ^/$ http://www.foo.com/foor_app

</VirtualHost>

<VirtualHost *:80>
    ServerName www.bar.com

    ProxyPreserveHost On

    ProxyPass /bar_app http://123.123.123.123:8180/bar_app
    ProxyPassReverse /bar_app http://176.34.212.135:8180/bar_app

    RewriteEngine on

    RewriteRule ^/$ http://www.bar.com/bar_app
</VirtualHost>

With ProxyPass, I always have the URL balanced on both sides - it may be possible to have them different but I never succeeded. The above is pretty much what I have on some of my servers.