skip to main | skip to sidebar

Friday, October 16, 2009

Hosting Multiple Domains In Tomcat

Tomcat allows us to host multiple domains in one instance, using multiple 'Host' tags. In this article I will explain how to do it on Tomcat. This is very simple configuration using 'Host' tags in your server.xml. A novice can also understand this configuration very easily.
Before going into the details of the configuration first lets have a look at the 'Host' tag, 'Context' tag and 'Alias' tags first.
   <Host name="domain1" appBase="[application base]" autoDeploy="[true/false]" 
        unpackWARs="[true/false]">
    <Alias>...</Alias>
    <Context path="" docBase="" reloadable="[true/false]"/>
   </Host>
  
First lets have a look at 'Alias' tag. This tag is used to provide aliases for your actual domain. For example you have a domain called 'domain1.com', and you want to run the same application for 'www.domain1.com' also, then you can use this 'Alias' tag to provide an alias name for the actual domain.
   <Host name="domain1.com" ...>
    <Alias>www.domain1.com</Alias>
    <Alias>domain1.net</Alias>
    <Alias>www.domain1.net</Alias>
    ...
   </Host>
  
As you can see in the above example, if you want to have multiple aliases you can add multiple 'Alias' tags for each domain alias name.
Now lets move on to the 'Context' tag. The 'Context' element represents a web application running inside a host. To explain this in an easy way; each directory under 'webapps' directory of your tomcat is one context. Manager and Admin consoles of your tomcat installation are two different contexts running under your 'localhost' domain.
   <Context path="" docBase="" reloadable="[true/false]"/>
  
In the above code snippet I've provided minimum configuration needed for a context. Lets go through the attributes of 'Context' tag.
'path' attribute - here we need to give the relative URL [to the host URL in which this Context is being configured] of the context. Say you want to run the application from "domain.com/beta" then the 'path' attribute needs to be "/beta".
'docBase' - the Document Base directory. Here we need to give the root directory for this context. This can be an absolute path to the directory/WAR file OR relative to the 'appBase' given in the 'Host' tag. If the context root directory is inside the 'appBase' directory of the 'Host' tag then we can give it as
   docBase="."
  
'reloadable', it defaults to 'false'. If you give 'true' to this value, tomcat looks for changes in the 'WEB-INF/classes' or 'WEB-INF/lib' directory and reloads the context automatically. This will useful in development environment, so that a new deployment doesn't result restarting tomcat. But on production server its better to leave the default value as setting it to true results an overhead on the server.
Now the 'Host' tag - it represents a host [also called as Virtual Host] running, associated with a domain name in the server. We can have multiple 'Host' tags to host multiple domains in one tomcat. Lets see the attributes.
   <Host name="domain1" appBase="[application base]" autoDeploy="[true/false]" 
        unpackWARs="[true/false]">
   ...
   <Host/>
  
'name' - Domain Name attribute. Here we need to give the domain name which you are trying to host/deploy.
'appBase' - Application Base Directory attribute. Here we need to give the root directory for this application which contains web applications to be deployed on this host. It can be either an absolute path to the directory OR relative to the 'CATALINA_BASE' directory.
'autoDeploy' - flag to denote newly placed web applications should be deployed automatically. If this attribute is set to true and you place a WAR file OR a Web application directory in 'appBase' then tomcat automatically deploys the application.
'unpackWARs' - if set to true Tomcat will automatically unpack the WAR files placed in to corresponding directory structure.
Now lets look at the configuration to host multiple domains.
   <Engine defaultHost="domain1.com" name="Catalina">
    <Host name="domain1.com" appBase="/home/user1/domain1">
     <Alias>www.domain1.com</Alias>
     <Context path="" docBase="."/>
    </Host>
    <Host name="domain2.com" appBase="/home/user1/domain2">
     <Alias>www.domain2.com</Alias>
     <Context path="" docBase="."/>
    </Host>
    <Host name="domain3.com" appBase="/home/user1/domain3">
     <Alias>www.domain2.com</Alias>
     <Context path="" docBase="."/>
    </Host>
   </Engine>
  
In the above configuration we tried to host three domains in Tomcat. We added three 'Host' tags one for each domain we wanted to host. Each domain points to a different ['appBase'] directory [I used absolute paths for 'appBase' attribute, it can be relative to CATALINA_BASE].
That's it for now!
Reference URLs

14 comments:

k1DBLITZ said...

Thanks. I'm new to Tomcat and this article helped me understand the terminology and how it applies to the configuration.

Rakesh Reddy said...

I am glad it helped you :)

WebDevelopment said...

Hey Rakesh,
I know, there's a long time ago you written it.
But really it helped me a lot.

Thank you so much.

Just one thing, can you report me if it works well on Tomcat 6?
(I hope so)

Rakesh A said...

@WebDevelopment - I didn't try it, but It should work in Tomcat6, I see no major differences in documentation.

anhngockiemtien said...

Thanks, helpful but not time-consuming in compared with reading all Tomcat Manual

Patrick M said...

thanks, that was really helpful..

do you know, how can I set up a redirect from a alias to a specific location (anchor) within my webapp?

i.e. some.domain.com -> domain.com#pageid=12

Rakesh A said...

@Patrick - I doubt that is possible with configuration, at least I dont know how to do it!

Rakesh A said...

@Patrick - after a brief search, I found one URL, which might give you some alternatives, here it is
http://stackoverflow.com/questions/3238517/how-do-i-redirect-a-domain-to-a-specific-landing-page

Rakesh A said...

@Patrick - http://www.webmasterworld.com/apache/3384249.htm

Kiran said...

Hi Rakesh,
Great Information. But I have one query I am not able to see my project in the tomcat manages as i have 2 web apps so could you please let me know how to map two webapps to 2 diffrent tomacats

maria peña said...

Hi, I learned so much with this info, thank yo so much. But I was wondering if I want to have 2 apps runnning in the same tomcat instance and both of them need to to be the root app, can I do It with this configuration?. For example I have domain1.com linked to the app that is in folder ROOT but I want the app that is in folder app1 to be linked to the domain2.com without putting a the URL domain2.com/app1.

Please help me

Rakesh A said...

@maria - Yes, if you follow the instruction in this post, you'll be able to configure it.

Tarun Kumar said...

I have deployed 3 web apps on tomcat.
My problem is that for each web app 4 ServletContext objects are getting created.
As per J2EE only 1 ServletContext object per web app should get created.

Rakesh A said...

@Tarun: You are right, as far as I know only one servlet context gets created per web app, how did you test and found that 4 servlet context objects are created?

Post a Comment