Tomcat建立虚拟主机最佳实践
博客分类: Tomcat
tomcatURL重写虚拟主机映射多个域名最佳实践
一、需求和解决思路:
在项目经验中,主机的TCP协议(HTTP协议是基于TCP/IP协议)的80端口是稀缺资源,很多情况下不可能为一个应用而去申请一台主机(不管是物理主机还是虚拟服务器)。这时候很多的Java Web应用会放到一个拥有TCP的80端口的Tomcat下面,由于每个应用都要有自己的域名地址(这里域名后面肯定不能有端口),所以需要我们配置该Tomcat来支持一个IP对应多个域名,甚至一个应用对应多个域名。
二、具体解决方案和步骤:
在开源社区里,一般我们会用linux+tomcat来部署JavaEE应用,这里我们的生产环境也是如此,但是我们可以在Windows的开发环境下完成上述需求。这里我们已经有一个安装好的Tomcat6.0,并且在tomcat目录下自行建立了一个webapps2目录,在webapps和webapps2目录下非别都放置了一个测试项目“6”和“7”。两个项目的web.xml中均配置了:
Xml代码 收藏代码
index.html
配置虚拟主机和URL重写的具体流程如下:
1.修改tomcat 6.0的默认访问端口(在conf目录的server.xml中),将8080改为80。
原始:
Xml代码 收藏代码
<?xml version='1.0' encoding='utf-8'?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- Note: A "Server" is not itself a "Container", so you may not
define subcomponents such as "Valves" at this level.
Documentation at /docs/config/server.html
-->
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<!-- 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" />
<!-- Global JNDI resources
Documentation at /docs/jndi-resources-howto.html
-->
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<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="www.aaa.com">
<Host name="www.aaa.com" appBase="webapps_1" autoDeploy="true" unpackWARs="true" xmlNamespaceAware="false" xmlValidation="false">
<Alias>aaa-com</Alias>
<Context path="" reloadable="true"
docBase="/test1.war"
workDir="" />
</Host>
<Host name="www.bbb.com" appBase="webapps_2" autoDeploy="true" unpackWARs="true" xmlNamespaceAware="false" xmlValidation="false">
<Context path="" reloadable="true"
docBase="/test2.war"
workDir="" />
</Host>
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
</Engine>
</Service>
</Server>