Search code examples
javatomcatvirtualhosttomcat8

Virtual Host in Tomcat 8


I want to map a domain mydomain.com to an application. I added a <Host> entry into server.xml, but when I go to www.mydomain.com it shows the Tomcat start page instead of my application.

server.xml:

<Engine name="Catalina" defaultHost="localhost">

  <Realm className="org.apache.catalina.realm.LockOutRealm">
   <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
  </Realm>

  <Host name="localhost" autoDeploy="true" unpackWARs="true" appBase="webapps">
    <Valve className="org.apache.catalina.valves.AccessLogValve" pattern="%h %l %u %t "%r" %s %b" suffix=".txt" prefix="localhost_access_log" directory="logs"/>
  </Host>


  <Host name="mydomain.com" autoDeploy="true" unpackWARs="true" appBase="webapps">
    <Alias>www.mydomain.com</Alias>
    <Context privileged="true" debug="0" docBase="/opt/tomcat/webapps/MyDomain" path=""/>
    <Valve className="org.apache.catalina.valves.AccessLogValve" pattern="%h %l %u %t "%r" %s %b" suffix=".txt" prefix="localhost_access_log." directory="logs" resolveHosts="false"/>
  </Host>

</Engine>

Thanks in advance!


Solution

  • You have defined two hosts with the same deployment directory webapps

    If you want localhost to be different than mydomain define two deployment directories and rename the root context war to ROOT.war. it is not recommended to use <Context tag

    <Host name="localhost" autoDeploy="true" unpackWARs="true" appBase="webapps">
    
    <Host name="www.mydomain.com" autoDeploy="true" unpackWARs="true" appBase="mydomain">
    </Host>
    

    You do not need alias since the host name contains the domain name. All others requests coming from a DNS entry different to www.mydomain.com will be served by localhost

    If localhost and www.mydomain.com are the same, then you just need a <Host and deploy a ROOT.war

     <Host name="localhost" autoDeploy="true" unpackWARs="true" appBase="webapps">
     </Host>