Tomcat多虚拟站点配置

参考资料:Apache Tomcat文档http://tomcat.apache.org/tomcat-5.0-doc/config/host.html
  在文中有这么一段话:
  One or more Host elements are nested inside an Engine element. Inside the Host element, you can nest Context elements for the web applications associated with this virtual host. Exactly one of the Hosts associated with each Engine MUST have a name matching the defaultHost attribute of that Engine.

  译文:Engine元素中需要一个或多个Host元素,在Host元素里面,你必需有Context元素让网站应用程序与虚拟主机连接上,严密地说,每一个主机所关联的引擎必须有一个名字跟那个引擎默认的主机属性匹配 .
  可知,在Engine元素里面可以有多个Host,那么说,可以有在一个Engine里面设置多个服务器了,这正是我们需要的.每个Host元素里面要有一个Context元素.
  根据conf/server.xml里面的说明和范例,我样可以编写出下面一个配置文件:

 
  可以看到,这里修改了
  81行修改了两个参数值:<Connector port="80" maxHttpHeaderSize="8192"
                maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
                enableLookups="false" redirectPort="8443" acceptCount="100"
                connectionTimeout="20000" disableUploadTimeout="true"  URIEncoding="GB2312"/>
          修改port是修改Tomcat的服务端口,默认为8080,URIEncoding改为GB2312是为了使用中文路径
    但不建议使用.

  125行:<Engine name="Catalina" defaultHost="ycoe.vicp.net">

        192行:<Host name="ycoe.vicp.net" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
  然后再添加360行开始的<Host>元素:<Host name="yvor.vicp.net" appBase="webapps"unpackWARs="true" autoDeploy="true"
        xmlValidation="false" xmlNamespaceAware="false">
    <Context docBase="D:/WORKS/YCOE/ycoe" path="/" reloadable="true" 
            workDir="D:/WORKS/YCOE/Tomcat/work/ycoe"></Context>
</Host>
  这里是设置我们的第二个虚拟网站的域名.
  注:<Context/>里面的内容并不是我们实际应用的,我们可以通过另一种比较方便而且容易修改的方式来设置这些参数.下面我们来做这方面的配置:
  1.在%CATALINA_HOME %/conf/Catalina目录下创建ycoe.vicp.net和yvor.vicp.net两个文件夹.
  2.在这两个文件夹里面创建ROOT.xml文件(要以ROOT.xml为名称,否则虽然不会出错,但不能用http://ycoe.vicp.nethttp://yvor.vicp.net直接访问)
  3.ROOT.xml的内容如下:
<?xml version='1.0' encoding='utf-8'?>
<Context docBase="D:/WORKS/EShop/EWebShop" path="/" reloadable="true" 
workDir="D:/WORKS/EShop/Tomcat/work/EWebShop">
</Context>

  根据自己的实际情况,设置这里的docBase 和workDir的路径.docBase是说明文档的路径,workDir是网站程序的路径,如果用相对路径,则是在%CATALINA_HOME %/webapp目录下,path是访问的路径

  参考官方文档:

Any XML file in the $CATALINA_HOME/conf/[engine_name]/[host_name] directory is assumed to contain a Context element (and its associated subelements) for a single web application. The docBase attribute of this <Context> element will typically be the absolute pathname to a web application directory, or the absolute pathname of a web application archive (WAR) file (which will not be expanded). 
Any web application archive file within the application base (appBase) directory that does not have a corresponding directory of the same name (without the ".war" extension) will be automatically expanded, unless the unpackWARs property is set to false. If you redeploy an updated WAR file, be sure to delete the expanded directory when restarting Tomcat, so that the updated WAR file will be re-expanded (note that the auto deployer will automatically take care of this if it is enabled). 
Any subdirectory within the application base directory that appears to be an unpacked web application (that is, it contains a /WEB-INF/web.xml file) will receive an automatically generated Context element, even if this directory is not mentioned in the conf/server.xml file. This generated Context entry will be configured according to the properties set in any DefaultContext element nested in this Host element. The context path for this deployed Context will be a slash character ("/") followed by the directory name, unless the directory name is ROOT, in which case the context path will be an empty string ("").

  你也可以在这两个目录下创建其它xml的文件

  但是这时你通过浏览器访问http://ycoe.vicp.nethttp://yvor.vicp.net时并不能浏览到你的网页,因为它把这些网址解析到广域网上去了,除非你用域名绑定.
  为了让局域本机不把这两个网址解析到广域网上去.我们可以通过以下设置实现(Windows XP,其它操作系统没有试过):
 1.用文本编辑器打开C:/WINDOWS/system32/drivers/etc目录的hosts文件
 2.在内容最后另起一行,添加以下内容:
            127.0.0.1       ycoe.vicp.net
            127.0.0.1       yvor.vicp.net

  可以由上面的注释部分了解它的作用:


# Copyright © 1993-1999 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
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host
  到这里,全部的配置已经完成了.重启Tomcat,打开http://ycoe.vicp.nethttp://yvor.vicp.net就可以看到预期的效果了.呵呵 
  下载相关文件http://www.cnblogs.com/Files/ycoe/Catalina.rar

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值