Main Content

Virtual Hosting under Tomcat - an example

Archive - Originally posted on "The Horse's Mouth" - 2008-11-22 01:52:12 - Graham Ellis

Tomcat supports "Virtual hosting", where you can direct several domain names to the same server computer / instance of Tomcat, and it will serve different content depending on which server you have asked even though they're on the same computer.

Here is the important part of the server.xml file that I used to set up an example yesterday afternoon:

<Engine name="Catalina" defaultHost="easterton">
  
<Host name="easterton" appBase="webapps"
  unpackWARs="true" autoDeploy="true"
  xmlValidation="false" xmlNamespaceAware="false">
  </Host>
  
<Host name="westerton" appBase="webapps2"
  unpackWARs="true" autoDeploy="true"
  xmlValidation="false" xmlNamespaceAware="false">
  </Host>
  
</Engine>


This Tomcat is serving a domain called "easterton" from the webapps folder, and a domain called "westerton" from the webapps2 folder. The domain names need to be correctly resolved by the browsing system (which will happen if you're using DNS, or if you set it in their /etc/hosts file). If the server receives an request for an unrecognised host, it defaults to "easterton".

Although I'm showing you virtual hosting on Apache Tomcat in thsi example, it's far more commonly used on Apache httpd - indeed, you'll probably find that more domains worldwide are served via a virtual host setup than via single computer hosting.

See the full Tomcat configuration file I was using here. For details of our Tomcat deployment courses, see here, and for Apache httpd see here