Tomcat的server.xml文件配置了tomcat的相关信息,下面对tomcat的server.xml文件进行分析可以详细的了解其配置以及可以很好的理解tomcat支持多虚拟域名映射的原理。
一、该xml文件配置的结构如图:
由该xml文件可以看出一个Server容器中包含三个主要部分,其中最核心的为Service部分,当浏览器发送一个请求给服务器时首先会被地址栏后的域名或Ip找到对应的Engine拦截:
<Service name="Catalina">
<Engine name="Catalina" defaultHost="localhost">
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
</Host>
<Host name="sso.com" appBase="suibian"
unpackWARs="true" autoDeploy="true"/>
</Engine>
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" useBodyEncodingForURI="true"/>
</Service>
其中关键的几点:
- Engin会被配置的defaultHost="localhost"根据地址栏找到,可以是Ip映射也可以是虚拟域名映射。同时根据Host后面指定的appBase寻找相对路径来作为要访问资源的根目录,其该路径是相对于tomcat的根目录即(bin的父目录为该目录)需要注意的是:当不是默认的webapps时里面的资源需要放在一文件夹下
- Engin会找跟自己名字相同的Service,对应的Service寻找其下配置的Connector根据端口号、以及协议版本来找到对应的Connector
其详细的执行过程可以为下图:
二、虚拟域名映射的详细过程
<Engine name="Catalina" defaultHost="localhost">
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true"/>
<Host name="sso.com" appBase="suibian"
unpackWARs="true" autoDeploy="true"/>
</Engine>
- 可以看到一个Engin下面可以配置多个Host并且根据不同的name映射一个不同的appBase我们将自己的不同的域名下要发布的资源上传到相应的appBase目录下即可。其中他们都是通过一个相同的Ip在访问。
- 虚拟域名与对应的Ip间的映射,在Windows环境下将虚拟域名与Ip映射起来即可。具体映射过程:
- 在Windows对应的:C:\Windows\System32\drivers\etc路径下找到hosts文件,打开配置响应的域名与ip对应关系
# Copyright (c) 1993-2009 Microsoft Corp. # # This is a sample HOSTS file used by Microsoft TCP/IP for Windows. # # This file contains the mappings of IP addresses to host names. Each # entry should be kept on an individual line. The IP address should # be placed in the first column followed by the corresponding host name. # The IP address and the host name should be separated by at least one # space. # # Additionally, comments (such as these) may be inserted on individual # lines or following the machine name denoted by a '#' symbol. # # For example: # # 102.54.94.97 rhino.acme.com # source server # 38.25.63.10 x.acme.com # x client host # localhost name resolution is handled within DNS itself. # 127.0.0.1 localhost # ::1 localhost 127.0.0.1 sso.com 127.0.0.1 shop.sso.com