21

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?

JoseK
  • 31,141
  • 14
  • 104
  • 131
Kawu
  • 13,647
  • 34
  • 123
  • 195
  • Hi Karsten, did your Tomcat start clean with the above config? Any errors in the catalina log? – codeporn Sep 14 '10 at 13:49
  • Clean from what I see. No errors in catalina.*.log, only a warning "14.09.2010 17:25:53 org.apache.catalina.startup.HostConfig deployDescriptor WARNUNG: A docBase C:\dev\tomcat\webapps\bbstats inside the host appBase has been specified, and will be ignored" which probably has to do with the context.xml specifying the same appBase and docBase values. – Kawu Sep 14 '10 at 15:28

1 Answers1

28

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>
OcuS
  • 5,320
  • 3
  • 36
  • 45
JoseK
  • 31,141
  • 14
  • 104
  • 131
  • 2
    I'm now using "" and it works now! :-) Note I've also removed path and docBase from my META-INF/context.xml. Quoting http://old.nabble.com/appbase-and-docbase-td18616249.html: "...the path and docBase attributes are not allowed for a element in META-INF/context.xml" I further found your technique confirmed by http://oreilly.com/java/archive/tomcat-tips.html. Cheers! – Kawu Sep 14 '10 at 15:57
  • 6
    Using the docBase as described above produced a `Document base /bbstats does not exist or is not a readable directory`, problem was resolved by removing the slash: ``. Works great, thanks! – Kof Jun 10 '12 at 08:12
  • 2
    Hi could you please guide me achieving sub domain configuration using tomcat for windows server – kakabali Mar 18 '14 at 18:29