Tomcat Virtual Host or Secondary Domain Name Configuration

Keywords: Apache Tomcat network xml

Project Host Configuration Based on Virtual Directory

In Tomcat, the default virtual directory is webapps, but for the project location, we can also set up Context to achieve, a Host can set up more than one Context;

<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
    <!--Host: name Express access to web sites,appBase Accessible directories for network addresses,unpackWARs Is decompression? war Software package, autoDeploy Are Web sites automatically deployed?>
<Context path="" docBase="." debug="0" reloadable="true"/>
   <!--Context It refers to the physical deployment environment. docBase Refers to the actual location of the site, here and appBase Forming virtual mapping,debug Whether to turn on debugging mode, reloadable Indicates whether redeployment is possible-->
<Context path="/bbs" docBase="E:/mytest/portal" debug="0" reloadable="true"/>
<Context path="/admin" docBase="E:/mytest/sys/admin" debug="0" reloadable="true"/>

<Logger className="org.apache.catalina.logger.FileLogger" directory="logs" prefix="www_appTest_com." suffix=".txt" 
    timestamp="true"/> 

</Host>

Virtual Host Configuration Based on Host Name

Modify the Tomcat configuration file / conf/server.xml to add a configuration similar to the following at the bottom of the Engine tag

<Host name="www.appTest.com" appBase="webapps/appTest" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
    <!--Host: name Express access to web sites,appBase Accessible directories for network addresses,unpackWARs Is decompression? war Software package, autoDeploy Are Web sites automatically deployed?>
   <Context path="" docBase="." debug="0" reloadable="true"/>
   <!--Context It refers to the physical deployment environment. docBase Refers to the actual location of the site, here and appBase Form virtual mapping,debug Whether to turn on debugging mode, reloadable Indicates whether redeployment is possible-->
   <Logger className="org.apache.catalina.logger.FileLogger" directory="logs" prefix="www_appTest_com." suffix=".txt" 
    timestamp="true"/> 
</Host>

 

When we don't have domain names, DNS parsing can't find the site, so we need to modify the hosts file to map the virtual address host IP.

201.18.2.102  www.appTest.com

Or in order to only allow LAN hosts access, you can set up the following settings to map your internal and external IPS

192.168.1.112 www.appTest.com

 

Of course, we can configure our subdomain name

<Host name="www.appTest.com" appBase="webapps/appTest" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
		<Context path="" docBase="." debug="0" reloadable="true"/>
		<Logger className="org.apache.catalina.logger.FileLogger" directory="logs" prefix="www_appTest_com." suffix=".txt" 
    timestamp="true"/> 
      </Host>

	<Host name="images.appTest.com" appBase="webapps/images" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
		<Context path="" docBase="." debug="0" reloadable="true"/>
		<Logger className="org.apache.catalina.logger.FileLogger" directory="logs" prefix="www_appTest_com." suffix=".txt" 
    timestamp="true"/> 
</Host>

Similarly, we need to map our web site.

201.18.2.112 images.appTest.com

Of course, when Cookie is cross-domain, we need to set Cookie Domain to. appTest.com.

Cookie cookie = new Cookie("cookieName", "cookieValue");
cookie.setDomain(".appTest.com");
response.addCookie(cookie);

 

Port-based Virtual Host Configuration

Add a configuration similar to the following under the server node

<Service name="myVirtualService">
    <Connector connectionTimeout="20000" port="8082" protocol="HTTP/1.1" redirectPort="8443"/> 
    <Connector port="8092" protocol="AJP/1.3" redirectPort="8443" />
    <Engine name="myVirtualServiceEngine" debug="0" defaultHost="localhost">
        <Host appBase="webapps2" autoDeploy="true" name="localhost" unpackWARs="true">
        	 <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access8082_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
        	  <Context path="" docBase="." debug="0"  reloadable="true"/>
        </Host>
    </Engine>
  </Service>

Posted by Wldrumstcs on Sun, 13 Oct 2019 11:27:47 -0700