jQuery in Action, 2nd edition*
The moose likes Tomcat and the fly likes Can Tomcat serve apps on two different IP addesses, both on port 80 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Can Tomcat serve apps on two different IP addesses, both on port 80" Watch "Can Tomcat serve apps on two different IP addesses, both on port 80" New topic
Author

Can Tomcat serve apps on two different IP addesses, both on port 80

Dave Knapp
Greenhorn

Joined: Feb 23, 2012
Posts: 4
We are trying to create a DNS name that points to a Java/Tomcat app. Since DNS cannot handle port numbers, my network guys tell me, I need to have some way for the Java app to respond only on the IP defined in DNS for that app. For instance, we have configured the DNS alias TestApp to point to 192.168.200.10. Can I configure Tomcat to fire up the TestApp application on port 80 of that address? The other apps in that Tomcat container will continue respond using the default IP that Tomcat recognized upon installation, i.e., 192.168.200.20/ProdApp1, 192.168.200.20/ProdApp2. BTW, we are not really using 192 addresses, that is just an example.

Our Tomcat is running on a Windows 2003 server with one NIC.

The point of this is to allow our corporate intranet users to just type, for example, http://TestApp and get to the desired application. They don't want to type http://TomcatServer/Apps/TestApp. Of course the real string is longer.
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 489
    
    5
Assuming your tomcat /webapps directory looks like this:


All you need to do to make a request to "http://TestApp" resolve to webapp "TestApp.war" is to rename "TestApp.war" as "ROOT.war" and make Tomcat listen on port 80 by specifying it in server.xml HTTP connector element (http://tomcat.apache.org/tomcat-6.0-doc/config/http.html).
Any webapp named "ROOT.war" will not need the webapp name in URL.
It's not necessary to have a separate IP. Both domains "TestApp" and "TomcatServer" can point to the same IP as long as that IP is accessible to all users of your webapp.

If for some reason, you want to have separate IPs (for example, same NIC with multiple IPs), then Tomcat by default binds to all available IPs on port 80 - you don't have to do anything special.

The side effect of all this is that http://TomcatServer (without any webapp name) too will resolve to "ROOT.war", and "http://TestApp/ProdApp1" will resolve to "ProdApp1.war".
If that is not what you want, then one solution is to use separate "Virtual hosts" (http://tomcat.apache.org/tomcat-6.0-doc/config/host.html) for each domain.
The Virtual Host concept is for multi tenancy in a shared Tomcat instance. It allows multiple domain names to share the same IP, but resolve to different sets of webapps. Each Virtual Host can then have its own webapps directory.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Are there still people out there who think they have to type URLs? Just send them an e-mail containing the long and complicated URL and tell them to click on the link. And surely they know how to use the "Favorites" menu in their browser?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15957
    
  19

Paul Clapham wrote:Are there still people out there who think they have to type URLs? Just send them an e-mail containing the long and complicated URL and tell them to click on the link. And surely they know how to use the "Favorites" menu in their browser?




Sometimes you're not in a position to do this. I've nailed myself trying to show off obscured apps to people when I couldn't remember the full URL and didn't happen to have had the foresight to "email myself" not was it a browser favorite on the device being used.

The biggest obstacle to putting a specific app on port 80 is that it's port 80. That means that a lot of systems would require Tomcat to run with root privileges, and thus become more vulnerable to security exploits.

Exactly one and only one application can listen on any given port, whether it's port 80 or port 8080 or whatever. So the second obstacle is that there cannot be an existing competitor for that port.

Tomcat can listen on as many ports as you want to configure in, so a virtualhost that owns a simple URL/port is not itself a major problem. However it's a lot easier in most cases to set up a proxy server to do that for you, thanks to the 2 obstacles I listed above. SO, for example, you could setup a copy of Apache, activate mod_proxy, define a virtual host or 2, and then people could use URLs such as "http://testserver/testapp1", where Apache proxies for something like "testserver3:8080/testapp1". You could also go the full virtual host route (http:/testserver), but that means more DNS work, and people can probably be trusted to remember "testserver/testapp". When they can be trusted to remember anything at all.


Customer surveys are for companies who didn't pay proper attention to begin with.
Dave Knapp
Greenhorn

Joined: Feb 23, 2012
Posts: 4
Paul Clapham wrote:Are there still people out there who think they have to type URLs? Just send them an e-mail containing the long and complicated URL and tell them to click on the link. And surely they know how to use the "Favorites" menu in their browser?


That was our first idea, just mail out a shortcut. Turns out the application owner just wants to tell sales folks, who are not known for their technical prowess, to just type 'TestApp' (not its real name) and magically the app would run in their browser. There are lots of these folks who don't have offices, are just in for the day, yada yada.
Dave Knapp
Greenhorn

Joined: Feb 23, 2012
Posts: 4
Karthik Shiraly wrote:Assuming your tomcat /webapps directory looks like this:


All you need to do to make a request to "http://TestApp" resolve to webapp "TestApp.war" is to rename "TestApp.war" as "ROOT.war" and make Tomcat listen on port 80 by specifying it in server.xml HTTP connector element (http://tomcat.apache.org/tomcat-6.0-doc/config/http.html).
Any webapp named "ROOT.war" will not need the webapp name in URL.
It's not necessary to have a separate IP. Both domains "TestApp" and "TomcatServer" can point to the same IP as long as that IP is accessible to all users of your webapp.

If for some reason, you want to have separate IPs (for example, same NIC with multiple IPs), then Tomcat by default binds to all available IPs on port 80 - you don't have to do anything special.

The side effect of all this is that http://TomcatServer (without any webapp name) too will resolve to "ROOT.war", and "http://TestApp/ProdApp1" will resolve to "ProdApp1.war".
If that is not what you want, then one solution is to use separate "Virtual hosts" (http://tomcat.apache.org/tomcat-6.0-doc/config/host.html) for each domain.
The Virtual Host concept is for multi tenancy in a shared Tomcat instance. It allows multiple domain names to share the same IP, but resolve to different sets of webapps. Each Virtual Host can then have its own webapps directory.


How do I get it to respond on a different IP from the one that is statically assigned to the single NIC? Do I need to do some sort of configuration to the NIC itself? I am guessing that just changes to Tomcat is not enough. Would not the nic with a static IP effectively block packets that do not have its IP address?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15957
    
  19

Applications listen to ports, not NICs. However, NICs only listen to broadcasts and to traffic specifically addressed to that NIC. But a NIC can be set up to listen at multiple IP addresses, and I have production servers that do so. I forget the proper term, but these other addresses are alias addresses and on the RedHat Linux platform, they're fairly easy to set up.
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 489
    
    5
How do I get it to respond on a different IP from the one that is statically assigned to the single NIC? Do I need to do some sort of configuration to the NIC itself?


In your post, you've mentioned OS is Windows 2003. Assigning multiple IPs to the same NIC is possible in Windows too, from "Network connection properties > Internet Protocol Version 4 Properties > Advanced" dialog.

On a different note, it's not clear to me why you would need a dedicated IP address for this webapp. If the problem is only to have a friendly URL without any webapp name in it, then the "ROOT.war" name - optionally with virtual host configured - should solve the problem. What benefit will a dedicated IP address provide in this situation?
Dave Knapp
Greenhorn

Joined: Feb 23, 2012
Posts: 4
Karthik Shiraly wrote:
How do I get it to respond on a different IP from the one that is statically assigned to the single NIC? Do I need to do some sort of configuration to the NIC itself?


In your post, you've mentioned OS is Windows 2003. Assigning multiple IPs to the same NIC is possible in Windows too, from "Network connection properties > Internet Protocol Version 4 Properties > Advanced" dialog.

On a different note, it's not clear to me why you would need a dedicated IP address for this webapp. If the problem is only to have a friendly URL without any webapp name in it, then the "ROOT.war" name - optionally with virtual host configured - should solve the problem. What benefit will a dedicated IP address provide in this situation?


Good question. We have multiple Java apps on this server, but they don't all require the friendly URL. Port 80 is already in use.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can Tomcat serve apps on two different IP addesses, both on port 80
 
Similar Threads
Can't access Tomcat 7 from network
How to set Alias name for URL or change URL
Doubt on Protocol & Port number
change from localhost
How to change application url?