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]" 
    <Context path="" docBase="" reloadable="[true/false]"/>
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 '', and you want to run the same application for '' also, then you can use this 'Alias' tag to provide an alias name for the actual domain.
   <Host name="" ...>
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 "" 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
'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]" 
'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="" name="Catalina">
    <Host name="" appBase="/home/user1/domain1">
     <Context path="" docBase="."/>
    <Host name="" appBase="/home/user1/domain2">
     <Context path="" docBase="."/>
    <Host name="" appBase="/home/user1/domain3">
     <Context path="" docBase="."/>
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


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. ->
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
Rakesh A said…
@Patrick -
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 linked to the app that is in folder ROOT but I want the app that is in folder app1 to be linked to the without putting a the URL

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?
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

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 submits th…

Simple AJAX [, JSP] Example

You will find many examples to learn how AJAX [Asynchronous Java Script And XML] works, but this one is different. Its one of the best way of using AJAX in web applications. The difference -  many of the examples you find uses Java Script to parse the AJAX response, this example avoids that parsing Java Script and uses JSP to generate the HTML and uses simple Java Script just to make the asynchronous call and show the response. It shows simple way of using JSP and AJAX with as much less Java Script as possible. This article wont go into details of what AJAX is and how to make AJAX calls, it explains one good way among many to use AJAX. Lets get on with it then!
The flow of this example is like this – A simple JSP page where user performs an action [button click in this example] to get list of employees, on this action we’ll make an Asynchronous call to a Servlet which will prepare the dummy employee data and sends that data to a JSP page. This JSP generates the HTML displaying the em…