首页 > 代码库 > Tomcat灵活配置多项目,多端口,多域名

Tomcat灵活配置多项目,多端口,多域名

Tomcat的配置都在Tomcat的安装目录的conf文件夹下的server.xml文件

最初内容:(去掉所有注释)

<?xml version="1.0" encoding="utf-8"?><Server port="8005" shutdown="SHUTDOWN">   <Listener className="org.apache.catalina.startup.VersionLoggerListener"/>    <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on"/>    <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="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"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b"/> </Host> </Engine> </Service> </Server>

默认的是8080端口

1.多个端口指向同一个应用程序

绑定多个Connector

在上面的xml中添加

<Connector port="8020" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443"/>

重新启动Tomcat,如果有服务要重启服务,就会发现也可以用127.0.0.1:8020访问8080的项目

2.配置多个端口,分别指向不同的应用程序

server.xml文件中每个Server都可以配置一个应用程序,可以配置多个Service

在Tomcat安装目录的文件夹下分别创建两个文件夹,名称为webapps1,webapps2,用于存放不同的应用程序

技术分享

在server.xml中添加配置:

  <Service name="Catalina1">     <Connector port="8021" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443"/>      <Engine name="Catalina1" defaultHost="localhost">       <Realm className="org.apache.catalina.realm.LockOutRealm">         <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>       </Realm>        <Host name="localhost" appBase="webapps1" unpackWARs="true" autoDeploy="true">         <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b"/>       </Host>     </Engine>   </Service>    <Service name="Catalina2">     <Connector port="8022" protocol="HTTP/1.1" connectionTimeout="20000" 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="webapps2" unpackWARs="true" autoDeploy="true">         <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b"/>       </Host>     </Engine>   </Service> 

分别绑定8021,8022端口,分别指向webapp1,webapp2应用程序

完整版:

技术分享
<?xml version="1.0" encoding="utf-8"?><Server port="8005" shutdown="SHUTDOWN">   <Listener className="org.apache.catalina.startup.VersionLoggerListener"/>    <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on"/>    <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="HTTP/1.1" connectionTimeout="20000" redirectPort="8443"/>      <Connector port="8020" 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">         <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b"/>       </Host>     </Engine>   </Service>    <Service name="Catalina1">     <Connector port="8021" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443"/>      <Engine name="Catalina1" defaultHost="localhost">       <Realm className="org.apache.catalina.realm.LockOutRealm">         <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>       </Realm>        <Host name="localhost" appBase="webapps1" unpackWARs="true" autoDeploy="true">         <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b"/>       </Host>     </Engine>   </Service>    <Service name="Catalina2">     <Connector port="8022" protocol="HTTP/1.1" connectionTimeout="20000" 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="webapps2" unpackWARs="true" autoDeploy="true">         <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b"/>       </Host>     </Engine>   </Service> </Server>
View Code

同样重启Tomcat,或者服务即可。

技术分享

技术分享

3.配置多个域名,分别绑定不同的应用程序

 

Tomcat灵活配置多项目,多端口,多域名