---
title: "tomcat的多域名、虚拟主机配置"
author: "g21121"
date: "2012-05-10T13:48:57Z"
tags: []
categories: ["企业架构"]
slug: "tomcat"
draft: false
lastmod: "2012-05-10T13:48:57Z"
id: 1521333
url: "https://286.iteye.com/blog/1521333"
---

众所周知apache可以配置多域名和虚拟主机，而且配置起来比较简单，但是项目用到的是tomcat，配来配去总是不成功。查了些资料才总算可以，下面就跟大家分享下经验。

很多朋友搜索的内容基本是告诉我们这么配置：

在Engine标签下增面积Host标签，如下：

```
<Host name="www.site1.com"  appBase="webapps"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">
</Host>
<Host name="www.site2.com"  appBase="/xxx/xxx/site2"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">
</Host>
```

 经过测试这样是不行的，只能找到[www.site1.com](http://www.site1.com)这个域名，因为在site1中appBase已经指定了默认路径，所以解析site2时就出错了，这时我们应该修改配置为如下：

```
<Host name="www.site1.com"  appBase="/xxx/xxx/site1"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">
<Context  path="" docBase="/xxx/xxx/site1"  reloadable="true"/>
</Host>
<Host name="www.site2.com"  appBase="/xxx/xxx/site2"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">
<Context  path="" docBase="/xxx/xxx/site2"  reloadable="true"/>
</Host>
```

 这样就可以同时访问多个域名而网站目录不通的网站了。

当然如果想加入虚拟路径的话只需要增加如下内容在Host标签中即可：

```
<Context  path="/xxx"  docBase="/xxx/xxx/xxx"  reloadable="true"/>
```

 这里如果添加在site1中，则访问如下网址[www.site1.com/xxx](http://www.site1.com/xxx)将会定向到本地/xxx/xxx/xxx这个目录。