Virtual Hosts and Contexts

Harry · 12 Sep 2026 · 11 views

Set Up a Virtual Host

Serve several domains from one Tomcat using <Host> elements in server.xml:

<Engine name="Catalina" defaultHost="localhost">
  <Host name="www.example.com" appBase="webapps" unpackWARs="true" autoDeploy="true">
    <Alias>example.com</Alias>
    <Context path="" docBase="/srv/apps/example" />
  </Host>
</Engine>

Terms

  • Host - maps a domain (with aliases) to a set of apps.
  • Context - one webapp; path is its URL prefix, docBase its filesystem location.
  • defaultHost - where unknown hostnames go.

Context Outside appBase

You can point a context at any directory, useful when apps live outside Tomcat:

<Host name="app.example.com" appBase="">
  <Context path="" docBase="/var/www/apps/store" reloadable="true"/>
</Host>

Order matters: an empty path="" context acts as the default app for that host.

Per-App Context Files

Put META-INF/context.xml inside the webapp, or drop appname.xml into conf/Catalina/localhost/ to configure a single context (data sources, listeners) without touching server.xml.

Key Points

  • Hosts = domains; Contexts = apps under a domain.
  • Aliases let the same host answer multiple names.
  • Per-app context.xml keeps data sources with the app.
Share this post:

Comments (0)

Please login or register to comment.