Forwarding Request to Different Ports Based on Domain

no158
no158 used Ask the Experts™
on
Hello All,
I once again need your expertise on the following, can you please help?

I've set up a server and I'd like to host two web applications (living on two different tomcats) from different ports; however I could not figure out how to forward the requests to a specific port based on the domain name.

In a nutshell I want www.firstdomain.com to go to 8080 and www.seconddomain.com to go to port 8081 seemlessly with sessions etc... (I do not want people to see the trailing port numbers by the way)

This is what I have so far:
I'm running on Ubuntu Server Edition (linux of course) with Apache 2 and two tomcats.
I've managed to have my tomcats listen to different ports already (tomcat1 is listening to 192.168.1.9:8080 and tomcat2 is listening to 192.168.1.9.8081).

I'm a newbie on this, and I'm not sure if I can do this with iptables or do I have to get dirty with VirtualHosts mod_proxy etc (which scares me).

Thank you in advance.
Best,
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
http://www.ex-parrot.com/pete/tomcat-vhost.html has a how to on virtual hosting with tomcat.
Top Expert 2010

Commented:
If both host names resolve to the same IP then a couple of Apache Name based Virtual hosts, along with a few proxy rules should do. If they're different IP's and your server has multiple NIC's the plane IP based Virtual hosts should suffice, see:

http://www.experts-exchange.com/Software/Server_Software/Web_Servers/Apache/Q_24830635.html

Author

Commented:
Yes, both will resolve to the same IP... What would those few Virtual Hosts and Proxy Rules would be?
If you can give me some examples I would appreciate it.

Would the following work? (Given the public ip is 12.34.56.78 and the local ip is 192.168.1.9)
<VirtualHost 12.34.56.78>
   ServerName www.mymaindomain.com
</VirtualHost>
 
<VirtualHost 12.34.56.78>
   ServerName www.firstdomain.com
   ProxyPass / 192.168.1.9:8080/
  ProxyPassReverse / 192.168.1.9:8080/
</VirtualHost>
 
<VirtualHost 12.34.56.78>
   ServerName www.seconddomain.com
   ProxyPass / 192.168.1.9:8081/
   ProxyPassReverse / 192.168.1.9:8081/
</VirtualHost>

Open in new window

Top Expert 2010
Commented:
That should do, but you may want to add site specific access and error logs, and possibly some local static content e.g. error pages, for when the Tomcat's are down, so you may end up with something like:
ErrorDocument 400 /badreq.html
ErrorDocument 401 /noauth.html
ErrorDocument 403 /forbid.html
ErrorDocument 404 /missing.html
ErrorDocument 500 /conferr.html
ErrorDocument 503 /disruption.html
 
NameVirtualHost 12.34.56.78
 
<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>
 
<VirtualHost 12.34.56.78>
   ServerName www.mymaindomain.com
   DocumentRoot /srv/www/mymaindomain
   ErrorLog  logs/mymaindomain_error_log
   CustomLog logs/mymaindomain_access_log common
   <Directory /srv/www/mymaindomain>
        Options Indexes FollowSymLinks
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>
</VirtualHost>
 
<VirtualHost 12.34.56.78>
   ServerName www.firstdomain.com
   DocumentRoot /srv/www/firstdomain
   ErrorLog  logs/firstdomain_error_log
   CustomLog logs/firstdomain_access_log common
   ProxyPass        / 192.168.1.9:8080/
   ProxyPassReverse / 192.168.1.9:8080/
   <Directory /srv/www/firstdomain>
        Options Indexes FollowSymLinks
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>
</VirtualHost>
 
<VirtualHost 12.34.56.78>
   ServerName www.seconddomain.com
   DocumentRoot /srv/www/mymaindomain
   ErrorLog  logs/seconddomain_error_log
   CustomLog logs/seconddomain_access_log common
   ProxyPass        / 192.168.1.9:8081/
   ProxyPassReverse / 192.168.1.9:8081/
   <Directory /srv/www/seconddomain>
        Options Indexes FollowSymLinks 
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>
</VirtualHost>

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial