通过配置tomcat的server.xml来实现多端口多应用:
<?xml version='1.0' encoding='utf-8'?>
<Server port="8005" shutdown="SHUTDOWN">
<!-- Security listener. Documentation at /docs/config/listeners.html
<Listener className="org.apache.catalina.security.SecurityListener" />
-->
<!--APR library loader. Documentation at /docs/apr.html -->
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
<Listener className="org.apache.catalina.core.JasperListener" />
<!-- Prevent memory leaks due to use of particular java/javax APIs-->
<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="8080" protocol="org.apache.coyote.http11.Http11AprProtocol" URIEncoding="UTF-8"
enableLookups="false"
acceptCount="500"
connectionTimeout="5000"
disableUploadTimeout="true" maxThreads="1500" maxSpareThreads="50" minSpareThreads="25"
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">
<Context docBase="/data/java/appstore-api" path="/" reloadable="false" />
</Host>
</Engine>
</Service>
<Service name="web">
<Connector port="8088" protocol="HTTP/1.1" maxThreads="700" acceptCount="100" connectionTimeout="2000" redirectPort="8445" URIEncoding="UTF-8"/>
<Engine name="web" defaultHost="localhost">
<Host name="localhost" appBase="webapp1">
<Context docBase="/data/java/appstore-web" path="" reloadable="true" />
</Host>
</Engine>
</Service>
<Service name="web2">
<Connector port="8089" protocol="HTTP/1.1" maxThreads="700" acceptCount="100" connectionTimeout="2000" redirectPort="8445" URIEncoding="UTF-8"/>
<Engine name="web2" defaultHost="localhost">
<Host name="localhost" appBase="webapp2">
<Context docBase="/data/java/appstore-web2" path="" reloadable="true" />
</Host>
</Engine>
</Service>
</Server>
1、按照上面的配置,启动tomcat,可以发现:
1)启动tomcat后,在tomcat配置目录:/usr/local/tomcat/conf下,除了默认的Catalina文件夹外,还会生成web1和web2两个文件夹。原因是Engine组件中,指定了所用的web1和web2。当然,我们可以将不同service组件的Engine name都指定成Catalina。
2)启动tomcat后,在tomcat目录:/usr/local/tomcat下,除了默认的webapps文件夹外,还会生成webapp1和webapp2两个目录。原因是Host组件中,指定了appBase伟webapp1和webapp2.当然,我们可以将不同service组件的Host appBase指定成默认的webapps。
2、配置说明:
1)appBase是虚拟主机存放webapp的目录,它可以是相对路径,也可以是绝对路径。如果是相对路径,则相对于$CATALINA_HOME,严格并准确地说是CATALINA_BASE。
2)path是URI的匹配路径,相当于nginx的location后的路径。tomcat要求每个虚拟主机必须配置一个空字符串的path,该条context作为URI无法被明确匹配时的默认context,它相当于nginx中location / {}的作用。
3)docBase则是每个webapp的存放目录(或者是已归档的war文件),它可以是相对路径,也可以是绝对路径,提供相对路径时它相对于appBase。该目录一般在appBase的目录下,但并不规定一定要放在appBase下。对于web服务来说,它相当于nginx的root指令,但对于webapp来说,一个context就相当于一个webapp,而docBase正是webapp的路径。
以下是指定相同的appBase的一个配置:
<Service name="Catalina">
<Connector port="8081" protocol="HTTP/1.1" maxThreads="700" acceptCount="100" connectionTimeout="2000" redirectPort="8445" URIEncoding="UTF-8"/>
<Engine name="Catalina" defaultHost="localhost">
<Host name="localhost" appBase="webapps">
<Context docBase="testjsp" path="" reloadable="true" />
</Host>
</Engine>
</Service>
<Service name="Catalina">
<Connector port="8082" protocol="HTTP/1.1" maxThreads="700" acceptCount="100" connectionTimeout="2000" redirectPort="8445" URIEncoding="UTF-8"/>
<Engine name="Catalina" defaultHost="localhost">
<Host name="localhost" appBase="webapps">
<Context docBase="testjsp" path="" reloadable="true" />
</Host>
</Engine>
</Service>
这里的name跟默认的Catalina一样,appBase也跟默认的webapps一样,当然建议docBase指定到别的目录,启动然后访问如下链接测试:
http://localhost:8081/jspIndex
http://localhost:8082/jspIndex