tomcat 虚拟主机

来源:互联网 发布:容柏生结构事务所 知乎 编辑:程序博客网 时间:2024/05/01 22:45

       <!-- Define the default virtual host -->
      <Host name="localhost" debug="0" appBase="webapps"
       unpackWARs="true" autoDeploy="true">
      <!-- Define the default virtual host -->
      <Host name="172.23.137.11" debug="0" appBase="webapps"
       unpackWARs="true" autoDeploy="true">

      <!-- Define the default virtual host -->
      <Host name="www.sitoto.net" debug="0" appBase="webapps"
       unpackWARs="true" autoDeploy="true">
参考如下:

     4、配置虚拟主机(Virtual Hosts)
  
  关于server.xml中“Host”这个元素,只有在你设置虚拟主机的才需要修改。虚拟主机是一种在一个web服务器上服务多个域名的机制,对每个域名而言,都好象独享了整个主机。实际上,大多数的小型商务网站都是采用虚拟主机实现的,这主要是因为虚拟主机能直接连接到Internet并提供相应的带宽,以保障合理的访问响应速度,另外虚拟主机还能提供一个稳定的固定IP。
  
  基于名字的虚拟主机可以被建立在任何web服务器上,建立的方法就是通过在域名服务器(DNS)上建立IP地址的别名,并且告诉web服务器把去往不同域名的请求分发到相应的网页目录。因为这篇文章主要是讲Tomcat,我们不准备介绍在各种操作系统上设置DNS的方法,如果你在这方面需要帮助,请参考《DNS and Bind》一书,作者是Paul Albitz and Cricket Liu (O'Reilly)。为了示范方便,我将使用一个静态的主机文件,因为这是测试别名最简单的方法。
  
  在Tomcat中使用虚拟主机,你需要设置DNS或主机数据。为了测试,为本地IP设置一个IP别名就足够了,接下来,你需要在server.xml中添加几行内容,如下:
  
  <Server port="8005"
  shutdown="SHUTDOWN" debug="0">
  <Service name="Tomcat-Standalone">
  <Connector className=
  "org.apache.coyote.tomcat4.CoyoteConnector"
  port="8080"
  minProcessors="5" maxProcessors="75"
  enableLookups="true"
  redirectPort="8443"/>
  <Connector className=
  "org.apache.coyote.tomcat4.CoyoteConnector"
  port="8443" minProcessors="5"
  maxProcessors="75"
  acceptCount="10" debug="0"
  scheme="https" secure="true"/>
  <Factory className="org.apache.coyote.
  tomcat4.CoyoteServerSocketFactory"
  clientAuth="false" protocol="TLS" />
  </Connector>
  <Engine name="Standalone"
  defaultHost="localhost" debug="0">
  <!-- This Host is the default Host -->
  <Host name="localhost"
  debug="0" appBase="webapps"
  unpackWARs="true" autoDeploy="true">
  <Context path="" docBase="ROOT" debug="0"/>
  <Context path="/orders"
  docBase="/home/ian/orders" debug="0"
  reloadable="true" crossContext="true">
  </Context>
  </Host>

  
  <!-- This Host is the first
  "Virtual Host":
http://www.example.com/ -->
  <Host name="
www.example.com"
  appBase="/home/example/webapp">
  <Context path="" docBase="."/>
  </Host>

  
  </Engine>
  </Service>
  </Server>
  
  Tomcat的server.xml文件,在初始状态下,只包括一个虚拟主机,但是它容易被扩充到支持多个虚拟主机。在前面的例子中展示的是一个简单的server.xml版本,其中粗体部分就是用于添加一个虚拟主机。每一个Host元素必须包括一个或多个context元素,所包含的context元素中必须有一个是默认的context,这个默认的context的显示路径应该为空(例如,path=””)。

原创粉丝点击