
Tomcat gives the possibility to host multiple domains in one server (instance), using multiple ‘Host’ tags.
I will demonstrate this setup by using ‘Host’ tags in the
our server.xml. Before diving into the configuration, we will examine the ‘Host’ tag, ‘Context’ tag and ‘Alias’ tags to enable us get a better understanding of the whole concept.
<Host name="techdirectarchive" appBase="[application base]" autoDeploy="[true/false]"
unpackWARs="[true/false]">
<Alias>...</Alias>
<Context path="" docBase="" reloadable="[true/false]"/>
</Host>
This is how the ‘Alias’ tag looks and used to provide aliases for your actual domain.
For example you have a domain called ‘techdirectarchive.com’, and you want to run the same application for http://www.techdirectarchive.net’, the you would have to use this ‘Alias’ tag to provide an alias name for the actual domain.
<Host name="domain1.com" ...>
<Alias>www.techdirectarchive.com</Alias>
<Alias>techdirectarchive.com</Alias>
<Alias>www.techdirectarchive.net</Alias>
...
</Host>
As shown above, to have multiple aliases you can add multiple ‘Alias’ tags for each domain alias name. Using ‘Context’ tag. A’Context’ element represents a web application running inside a host. where 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 snippet shown above, this is a very basic or minimum configuration needed for a context. Here are the attributes relating to ‘Context‘ tag. Let’s discuss them in details.
Path Attribute:
This is the relative URL [to the host URL in which this Context is being configured] of the context. For example, you want to run the application from “techdirectarchive.com/easy” then the ‘path’ attribute needs to be “/beta”.
DocBase‘ – The document Base directory as the name implies, the root directory for this context is given and 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 shown below.
docBase="."
‘Reloadable: This defaults to ‘false’ if you give the value ‘true’. Tomcat looks for changes in the ‘WEB-INF/classes’ or ‘WEB-INF/lib’ directory and reloads the context automatically.
Note:
– 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. review the attributes associated.
<Host name="techdirectachive" appBase="[application base]" autoDeploy="[true/false]"
unpackWARs="[true/false]">
...
<Host/>
Name: Here you would simply give the Domain Name attribute which you want to 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:
This flag denotes the newly placed web applications that 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. Lets see the configuration needed to host multiple domains.
<Engine defaultHost="techdirectarchive.com" name="Catalina">
<Host name="techdirectarchive.com" appBase="/home/user1/techdirectarchive">
<Alias>www.techdirectarchive.com</Alias>
<Context path="" docBase="."/>
</Host>
<Host name="techdirectarchive2.com" appBase="/home/user1/techdirectarchive2">
<Alias>www.techdirectarchive2.com</Alias>
<Context path="" docBase="."/>
</Host>
<Host name="techdirectarchive3.com" appBase="/home/user1/techdirectarchive3">
<Alias>www.techdirectarchive2.com</Alias>
<Context path="" docBase="."/>
</Host>
</Engine>
Here, three domains were hosted and tested in Tomcat and three host tags used for each hoisted domain. Every domain pointed to a different [‘appBase’] directory. Absolute paths for ‘appBase’ attributes were used as well.
Note: You can as well use a relative path to CATALINA_BASE. This works too as tested.
https://stackoverflow.com/questions/9424554/one-tomcat-instance-for-two-domains-and-two-webapps