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 "%r" %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 "%r" %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 "%r" %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 "%r" %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 "%r" %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 "%r" %s %b"/> </Host> </Engine> </Service> </Server>
同样重启Tomcat,或者服务即可。
3.配置多个域名,分别绑定不同的应用程序
修改host的配置
<Service name="Catalina3"> <Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443"/> <Engine name="Catalina3" defaultHost="www.bbb.com"> <Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <Host name="www.bbb.com" 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 "%r" %s %b"/> </Host> <Host name="www.ddd.com" 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 "%r" %s %b"/> </Host> </Engine> </Service>
这里只是绑定了域名,本机调用的话,必须要修改hosts
4.虚拟目录
Host节点内部可以添加Context
<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"> <Context path="" docBase="/test/" debug="0" privileged="true"/> <Context path="aa" docBase="/test/" debug="0" privileged="true"/> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> </Engine> </Service>
docBase是虚拟目录的物理路径,可以设置相对路径,也可以设置绝对路径,相对路径是相对应appBase的
分别添加了两个Context,在webapps2文件夹下创建文件夹test,并在内部放入a.html
访问方式:
http://127.0.0.1:8022/test/a.html
http://127.0.0.1:8022/aa/a.html
http://127.0.0.1:8022/a.html
上面分别是3种不同的调用方式,都是调用同一个页面webapps2/test/a.html
5.指向开发项目目录
D:\代码\Git\SpringMVCDemo\target,这个是开发项目打包目录地址
springmvcdemo是项目名称,target文件夹下在打包后会生成springmvcdemo.war包,并自动解压。
<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="D:/代码/Git/SpringMVCDemo/target" unpackWARs="true" autoDeploy="true"> <Context path="" docBase="springmvcdemo" debug="0" privileged="true"/> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b"/> </Host> </Engine> </Service>
http://127.0.0.1:8022/springmvcdemo/admin/users
http://127.0.0.1:8022/admin/users
可以通过上面两个地址进行访问。
http://www.cnblogs.com/younggun/archive/2013/10/04/3351193.html