---
title: "tomcat  多个项目  子域名"
author: "af8991"
date: "2015-09-18T11:33:53Z"
tags: []
categories: ["互联网"]
slug: "tomcat"
draft: false
lastmod: "2015-09-18T11:33:53Z"
id: 2244460
url: "https://af8991.iteye.com/blog/2244460"
---

?

说明：

1.首先需要有多个域名，同时指向一个IP地址。

例如：[域名](http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=8&fv=18&is_app=0&jk=68b63273f1030a41&k=%D3%F2%C3%FB&k0=%D3%F2%C3%FB&kdi0=0&luki=6&n=10&p=baidu&q=sayyescpr&rb=0&rs=1&seller_id=1&sid=410a03f17332b668&ssp2=1&stid=0&t=tpclicked3_hc&td=1366390&tu=u1366390&u=http%3A%2F%2Fblog.knowsky.com%2F194729.htm&urlid=0)：[www.bbb.com](http://www.bbb.com/), [www.aaa.com](http://www.aaa.com/)等等。

解析到一个公网IP地址：210.34.120.110.

2.需要对tomcat下的conf下的"[server](http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=8&fv=18&is_app=0&jk=68b63273f1030a41&k=server&k0=server&kdi0=0&luki=8&n=10&p=baidu&q=sayyescpr&rb=0&rs=1&seller_id=1&sid=410a03f17332b668&ssp2=1&stid=0&t=tpclicked3_hc&td=1366390&tu=u1366390&u=http%3A%2F%2Fblog.knowsky.com%2F194729.htm&urlid=0).xml"进行配置  
&lt;Server port="8005" shutdown="SHUTDOWN"&gt;

&lt;Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /&gt;  
&lt;Listener className="org.apache.catalina.core.JasperListener" /&gt;  
&lt;Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" /&gt;  
&lt;Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /&gt;

&lt;GlobalNamingResources&gt;  
&lt;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" /&gt;  
&lt;/GlobalNamingResources&gt;

&lt;Service name="Catalina"&gt;  
&lt;Connector port="8080" protocol="HTTP/1.1"  
connectionTimeout="20000"  
redirectPort="8443" URIEncoding="gbk" /&gt;  
&lt;Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /&gt;

&lt;!--以下配置是关键--&gt;  
&lt;Engine name="Catalina" defaultHost="[localhost](http://www.myself.com/)"&gt;

&lt;!--默认通过localhost转发--&gt;  
&lt;Realm className="org.apache.catalina.realm.UserDatabaseRealm"  
resourceName="UserDatabase"/&gt;

&lt;!--以下是自带的转发--&gt;  
&lt;Host name="localhost" appBase="webapps"  
unpackWARs="true" autoDeploy="true"  
xmlValidation="false" xmlNamespaceAware="false"&gt;

&lt;/Host&gt;  
&lt;!-- 在tomcat安装目录下新建文件夹aaa 与webapp同级，然后在该目录下新建Root,项目内容可放在这个位置 --&gt;  
&lt;Host name="[www.aaa.com](http://www.aaa.com/)" appBase="aaa"  
unpackWARs="true" autoDeploy="true"  
xmlValidation="false" xmlNamespaceAware="false"&gt;  
&lt;/Host&gt;

&lt;!-- 同上，在tomcat安装目录下新建[文件夹](http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=8&fv=18&is_app=0&jk=68b63273f1030a41&k=%CE%C4%BC%FE%BC%D0&k0=%CE%C4%BC%FE%BC%D0&kdi0=0&luki=4&n=10&p=baidu&q=sayyescpr&rb=0&rs=1&seller_id=1&sid=410a03f17332b668&ssp2=1&stid=0&t=tpclicked3_hc&td=1366390&tu=u1366390&u=http%3A%2F%2Fblog.knowsky.com%2F194729.htm&urlid=0)bbb与webapp同级 ， 然后在该目录下新建Root,项目内容可放在这个位置 --&gt;

&lt;Host name="[www.bbb.com](http://www.bbb.com/)" appBase="bbb"  
unpackWARs="true" autoDeploy="true"  
xmlValidation="false" xmlNamespaceAware="false"&gt;  
&lt;/Host&gt;

&lt;/Engine&gt;  
&lt;/Service&gt;  
&lt;/Server&gt;  
这样，就可以访问多个项目，通过不同的[域名](http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=8&fv=18&is_app=0&jk=68b63273f1030a41&k=%D3%F2%C3%FB&k0=%D3%F2%C3%FB&kdi0=0&luki=6&n=10&p=baidu&q=sayyescpr&rb=0&rs=1&seller_id=1&sid=410a03f17332b668&ssp2=1&stid=0&t=tpclicked3_hc&td=1366390&tu=u1366390&u=http%3A%2F%2Fblog.knowsky.com%2F194729.htm&urlid=0)进行。

附：如果进行本地测试。则需加如下配置：

C:\\Windows\\System32\\drivers\\etc\\hosts(Vista系统下路径，其他操作系统可能不一样)

\# Copyright (c) 1993-2006 Microsoft Corp.  
\#  
\# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.  
\#  
\# This file contains the mappings of IP addresses to host names. Each  
\# entry should be kept on an individual line. The IP address should  
\# be placed in the first column followed by the corresponding host name.  
\# The IP address and the host name should be separated by at least one  
\# space.  
\#  
\# Additionally, comments (such as these) may be inserted on individual  
# [line](http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=8&fv=18&is_app=0&jk=68b63273f1030a41&k=line&k0=line&kdi0=0&luki=2&n=10&p=baidu&q=sayyescpr&rb=0&rs=1&seller_id=1&sid=410a03f17332b668&ssp2=1&stid=0&t=tpclicked3_hc&td=1366390&tu=u1366390&u=http%3A%2F%2Fblog.knowsky.com%2F194729.htm&urlid=0)s or following the [machine](http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=8&fv=18&is_app=0&jk=68b63273f1030a41&k=machine&k0=machine&kdi0=0&luki=1&n=10&p=baidu&q=sayyescpr&rb=0&rs=1&seller_id=1&sid=410a03f17332b668&ssp2=1&stid=0&t=tpclicked3_hc&td=1366390&tu=u1366390&u=http%3A%2F%2Fblog.knowsky.com%2F194729.htm&urlid=0) name denoted by a '#' [symbol](http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=8&fv=18&is_app=0&jk=68b63273f1030a41&k=symbol&k0=symbol&kdi0=0&luki=3&n=10&p=baidu&q=sayyescpr&rb=0&rs=1&seller_id=1&sid=410a03f17332b668&ssp2=1&stid=0&t=tpclicked3_hc&td=1366390&tu=u1366390&u=http%3A%2F%2Fblog.knowsky.com%2F194729.htm&urlid=0).  
\#  
\# For example:  
\#  
\# 102.54.94.97 rhino.acme.com # source [server](http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=8&fv=18&is_app=0&jk=68b63273f1030a41&k=server&k0=server&kdi0=0&luki=8&n=10&p=baidu&q=sayyescpr&rb=0&rs=1&seller_id=1&sid=410a03f17332b668&ssp2=1&stid=0&t=tpclicked3_hc&td=1366390&tu=u1366390&u=http%3A%2F%2Fblog.knowsky.com%2F194729.htm&urlid=0)  
\# 38.25.63.10 x.acme.com # x client host

127.0.0.1 [www.aaa.com](http://www.aaa.com/)

127.0.0.1  [www.bbb.com](http://www.bbb.com/)

127.0.0.1 localhost