测试了一下tomcat下配置虚拟目录、多域名、多个Http监听端口的配置方式,下面做一个conf/server.xml配置结果的记录
< Listener className ="org.apache.catalina.core.AprLifecycleListener" SSLEngine ="on" />
< Listener className ="org.apache.catalina.core.JasperListener" />
< Listener className ="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
< Listener className ="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
< Listener className ="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
< GlobalNamingResources >
< Resource name ="UserDatabase" auth ="Container"
type ="org.apache.catalina.UserDatabase"
description ="User database that can be updated and saved"
factory ="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname ="conf/tomcat-users.xml" />
</ GlobalNamingResources >
< Service name ="Catalina" >
< Connector port ="8088" protocol ="HTTP/1.1"
connectionTimeout ="20000"
redirectPort ="8443" />
< Connector port ="8009" protocol ="AJP/1.3" redirectPort ="8443" />
< 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" appBase ="webapps"
unpackWARs ="true" autoDeploy ="true" >
< Alias > www.test3.com </ Alias >
< Valve className ="org.apache.catalina.valves.AccessLogValve" directory ="logs"
prefix ="localhost_access_log." suffix =".txt"
pattern ="%h %l %u %t "%r" %s %b" />
< Context path ="" docBase ="D:\TomCat7.0\testapp\TestWebliu" />
</ Host >
< Host name ="www.test1.com" appBase ="testapp"
unpackWARs ="true" autoDeploy ="true" >
</ Host >
</ Engine >
</ Service >
< Service name ="Catalina2" >
< Connector port ="880" protocol ="HTTP/1.1"
connectionTimeout ="20000"
redirectPort ="8443" />
< Connector port ="8010" protocol ="AJP/1.3" redirectPort ="8443" />
< Engine name ="Catalina2" defaultHost ="localhost" >
< Realm className ="org.apache.catalina.realm.LockOutRealm" >
< Realm className ="org.apache.catalina.realm.UserDatabaseRealm"
resourceName ="UserDatabase" />
</ Realm >
< Host name ="localhost" appBase ="webapps"
unpackWARs ="true" autoDeploy ="true" >
< Valve className ="org.apache.catalina.valves.AccessLogValve" directory ="logs"
prefix ="localhost_access_log." suffix =".txt"
pattern ="%h %l %u %t "%r" %s %b" />
< Context path ="" docBase ="D:\TomCat7.0\testapp\TestWebliu" />
</ Host >
< Host name ="www.test2.com" appBase ="testapp"
unpackWARs ="true" autoDeploy ="true" >
< Context path ="/TestWeb2" docBase ="D:\TomCat7.0\testapp\TestWeb2" />
< Context path ="" docBase ="D:\TomCat7.0\testapp\TestWebliu" />
</ Host >
</ Engine >
</ Service >
</ Server >
该server下面两个service节点,每个service节点可以用 Connector 配置一个监听端口,service里有只能有一个Engine节点,它接受同级目录Connector端口的请求,可以通过defaultHost属性默认指向一个Host,Host就是一个该Connector端口下的域名,下面可以用Context配置多个虚拟目录。
即:server下面可以有多个service,用于配置不同监听端口
service下面可以有多个Host,用于配置该端口下的不同域名
Host里可以包含多个Context,用于配置该端口该域名下的不同虚拟目录
上面的配置,可以用下面链接访问:
http://www.test1.com:8088/TestWeb2/
test1,2,3都是我hosts里指向了本地的。
Host下的Alias是一个域名别称,可以配置多个域名。
Host里如果没有Context节点,则非webapps文件夹下,直接不用虚拟目录访问,会抛404找不到文件的错误,比如上面配置中test1域名下必须用虚拟目录比如这里的TestWeb2来访问
Host下面的context节点配置为:<Context path="" 后,则这个目录为默认的访问目录,比如 http://www.test2.com:880/ ,访问的 D:\TomCat7.0\testapp\TestWebliu 下面的文件。
这里因为我有默认首页,因此直接输入域名和端口可以直接访问到默认的index.jsp页面,如果没有默认页,可能会抛错,需要配置可以/为列目录,在conf/web.xml里
<init-param>
<param-name>listings</param-name>
<param-value>true</param-value>
</init-param>
把这个的param-value改为true即为可以列目录了。
当然,如果不配置server.xml文件,直接把网站放到webapps,默认会自动加载该网站,因此大多数情况,我们都不用对server.xml文件做任何修改。