Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

up vote 5 down vote accepted

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.

share|improve this answer
    
Sorry I am a bit clueless at all this. What do you actually mean by "Make sure you have the mods enabled (proxy, proxy_html)" ? –  Phil Apr 17 '13 at 20:35
    
See edits - i expanded on that. –  ETL Apr 17 '13 at 20:42
    
Alas it doesn't seem to work. I removed the entry from iptables. Now when I go to www.foo.com I get an automatic redirect to www.foo.com/foo.app - but this results in a 404. If I change the URL to www.foo.com:8080/foo.app then the application is loaded. As I said in the original question I want the redirect from port 80 to port 8080 (or port 8180) to happen without the user having to enter it into their URL. –  Phil Apr 17 '13 at 21:35
    
Argh! This feels so close. I added a trailing slash to the entries of ProxyPass and ProxyPassReverse and the applications are attempting to start BUT they always fail with a "double name". So, going to www.foo.com takes me to www.foo.com/foo_appfoo_app. I then removed the trailing /<app name> from ProxyPass and ProxyPassReverse. Now going to www.foo.com has no effect BUT www.foo.com/my_app does start the application successfully. This is a partial success! But why should this behaviour occur? –  Phil Apr 17 '13 at 21:59
    
See further edits. –  ETL Apr 17 '13 at 22:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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