tomcat中Host的配置,实现同一个tomcat在共享IP时可以添加多个域名对应多个站点应用
为了本地测试,使用windows中的hosts添加两个域名
<!-- lang: shell -->
127.0.0.1 www1.localhost.com
127.0.0.1 www2.localhost.com
在server.xml中配置:
<!-- lang: xml -->
<Host name="www1.localhost.com" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="www1.localhost.com"/>
</Host>
<Host name="www2.localhost.com" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="www2.localhost.com"/>
</Host>
如果是linux下doc目录写文件系统路径docBase='/data/tomcat/www1.localhost.com/' 在webapps的目录下新建两个目录: www1.localhost.com和www2.localhost.com 两个文件夹下分别建立index.html,里面写上:hello www1.localhost.com
启动tomcat测试:
注:也可以整一个nginx或者apache在tomcat容器前面,这样一来域名分配方便,二来可以让nginx处理静态资源减轻tomcat压力。如果将来想要做集群也非常方