要求:同一台Tomcat
目标:
1.通过 localhost/项目名 访问Tomcat的webapp下的项目
2.通过 www.aaa.com 和 www.bbb.com 分别访问Tomcat下的不同域名的虚拟主机
一、修改server.xml文件下的端口号
二、项目位置
三、多域名配置
将server .xml文件中的<Host’>‘信息复制粘贴在’<‘engine>’</‘engine> 标签之中,并修改如下,其中参数name默认为localhost,修改为你自己的域名 并且在’<‘host>’</‘host> 标签之间添加’<‘Context docBase=“项目路径” path="" reloadable=“true” > 标签,’<'Context>中参数path为你的项目根目录,如项目在该tomcat下直接空着就行,docBase是基于appBase下的,可以写绝对路径,也可以写相对路径,可以把项目放到ROOT文件夹下,这样地址就不用带项目名
其实,context标签只是起到连接作用,不用也可以,只需把项目的内容(即除去外层的项目名文件夹)直接放到ROOT下运行就可以了,效果一样
<Engine name="Catalina" defaultHost="localhost">
<!--For clustering, please take a look at documentation at:
/docs/cluster-howto.html (simple how to)
/docs/config/cluster.html (reference documentation) -->
<!--
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
-->
<!-- Use the LockOutRealm to prevent attempts to guess user passwords
via a brute-force attack -->
<Realm className="org.apache.catalina.realm.LockOutRealm">
<!-- This Realm uses the UserDatabase configured in the global JNDI
resources under the key "UserDatabase". Any edits
that are performed against this UserDatabase are immediately
available for use by the Realm. -->
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
-->
<!--<Context path="" docBase="hugh" debug="0" reloadable="true" crossContext="true" />-->
<!-- Access log processes all example.
Documentation at: /docs/config/valve.html
Note: The pattern used is equivalent to using pattern="common" -->
<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.aaa.com" appBase="aaa"
unpackWARs="true" autoDeploy="true">
<Context path="" docBase="ROOT\AAA" debug="0" reloadable="true" crossContext="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.bbb.com" appBase="bbb"
unpackWARs="true" autoDeploy="true">
<Context path="" docBase="ROOT\BBB" debug="0" reloadable="true" crossContext="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>
四、域名解析
由于没有买域名,就只能通过修改hosts文件使用单机版玩玩
hosts路径 :C:\Windows\System32\Drivers\etc\hosts
五、让域名解析生效
重启或用cmd命令运行 ipconfig /flushdns
六、测试