How do you map a sub domain to a webapp on Tomcat 6 (including the root webapp)?

Kawu picture Kawu · Sep 14, 2010 · Viewed 23.2k times · Source

subject says it all. What I want is to map each sub domain to a webapp like:

http://root.domain.com:8080 -> http://domain.com:8080/
http://manager.domain.com:8080 -> http://domain.com:8080/manager
http://abc.domain.com:8080 -> http://domain.com:8080/abc
http://def.domain.com:8080 -> http://domain.com:8080/def

on a localhost machine this would be

http://root.localhost:8080 -> http://localhost:8080/
http://manager.localhost:8080 -> http://localhost:8080/manager
http://abc.localhost:8080 -> http://localhost:8080/abc
http://def.localhost:8080 -> http://localhost:8080/def

Ideally, I'd like to use port 80 instead of 8080, but that's another story. I'd be happy to get it going with port 8080 at first, so that the path at the end of the URL disappears.

Note, the arrows aren't redirects but what I'd enter if I left Tomcat as is.

I know the Tomcat docs page http://tomcat.apache.org/tomcat-6.0-doc/virtual-hosting-howto.html. I've read it many times, but didn't make much progress. I edited etc/hosts to add 127.0.0.1 bbstats.localhost. I then added

<Host name="bbstats.localhost" appBase="webapps/bbstats"
      unpackWARs="true" autoDeploy="true"
      xmlValidation="false" xmlNamespaceAware="false">
</Host>

to Tomcat's server.xml in the conf dir. My webapp's context.xml is:

<Context path="/bbstats" docBase="bbstats" debug="5" reloadable="true" crossContext="true"> 
</Context>

Restart Tomcat. Redploy via Ant. When entering

http://bbstats.localhost:8080/

into a browser, I get a blank screen.

When using appBase="webapps" instead of appBase="webapps/bbstats", I get to Tomcat's root app. The latter behavior is kind of expected. But how do I make bbstats.localhost:8080 go to the bbstats webapp without a trailing /bbstats in the URL?

Answer

JoseK picture JoseK · Sep 14, 2010

Can you try nesting each web app as the root webapp within the <Host> in server.xml by giving path="". I havent tried this myself.

<Host name="bbstats.localhost" appBase="webapps">
    <Context path="" docBase="/bbstats/"/>
</Host>

<Host name="tomcatstuff.localhost" appBase="webapps">
    <Context path="" docBase="/tomcatstuff/"/>
</Host>