Skip to main content

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

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 A said…
I am glad it helped you :)
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
tas 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
Unknown 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.
Unknown 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?
Any idea if this would work in Tomcat 7.x? Because I tried and failed. In fact, I didn't succeed in doing the same in Tomcat 6.0.

Popular posts from this blog

Simple Accordion using Java Script and CSS

Well you can find many online, but it's difficult to find one with out any dependent API. Most of the available accordions use other APIs for animation and other stuff. For those who just want accordion with out any other stuff, this one is the perfect one. It's very simple and you don't have to be a geek to understand it. Basic knowledge of Java script and CSS will do to understand how this works. In this article I'll take you through the steps of developing an accordion. Couple of minutes you are ready to write your own. Well then let's start developing one. Layout of the HTML block looks something like the one below Lets look at the CSS first which is very simple. /** container styles **/ .ra-p, .ra-cp { padding: 0 0 0.1em; } /**** heading styles ****/ .ra-p h2, .ra-cp h2 { margin: 0px; padding: 0.2em; cursor: pointer; color: #fff; background-color: #3d80b0; } /**** collapsed heading styles ****/ .ra-cp h...

File Uploading Using Servlets, JSP and Commons File Upload API

I’ve seen many developers who are at the early stages of their career have problems with this topic and seen many posts in forums asking how to do it – File Uploading using Servlets, JSP!; this article will provide an example using Commons File Upload API. I tried to make the example as simple as possible, hope it helps those early birds. Example uses JSP to provide the pages with form where user can select the file to upload with other form fields, Commons File Upload API to process submitted form and read form fields separately, and Servlets as middle layer between JSP and Commons File Upload API, example also has ANT build script to generate the distributables. All the code can be downloaded, links to these resources are provided at the end of this post, lets get on with the example then. The flow in this example is as depicted in the following picture. As you can see, user selects the file to upload and provides normal form data as well, using "index.jsp" and submi...