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.
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.
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.
'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
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.
'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.
That's it for now!
Reference URLs
<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
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)
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
http://stackoverflow.com/questions/3238517/how-do-i-redirect-a-domain-to-a-specific-landing-page
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
Please help me
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.
Post a Comment