Tomcat虚拟主机配置示例

来源:互联网 发布:二手mac pro工作站 编辑:程序博客网 时间:2024/06/10 06:10

Contexts are normally located underneath the appBase directory. For example, to deploy thefoobar context as a war file in the ren host, use $CATALINA_HOME/renapps/foobar.war. Note that the default or ROOT context forren would be deployed as $CATALINA_HOME/renapps/ROOT.war (WAR) or$CATALINA_HOME/renapps/ROOR (directory).


  意思也就是说,如果要配置成直接使用http://localhost/的方式进行访问,

要将war包命名为ROOT,同时根据文档:

 

Within your Context, create a META-INF directory and then place your Context definition in it in a file namedcontext.xml. i.e. $CATALINA_HOME/renapps/ROOT/META-INF/context.xml This makes deployment easier, particularly if you're distributing a WAR file.

这样也就是说,在WEB-INF/META-INF下面,需要添加一个context.xml文件,这样做的主要目的一方面,是希望单独web应用的修改,不用重启tomcat服务器,就可以将新配置的修改 重新加载,另一方面,当然是希望各个应用的配置可以相互,隔离.

整个tomcat配置虚拟主机的官方文档在这里可以找到.

http://tomcat.apache.org/tomcat-6.0-doc/virtual-hosting-howto.html

经过反复尝试,最终得如下步骤:

 1:修改/tomcat6/conf/server.xml

    主要修改为:

    <Engine name="Catalina" defaultHost="localhost">

 这里的defaultHost 要与下面的 hostname 保持一致

name要与接下来创建的目录名一致

 

<Host name="localhost"  appBase="F:"WebDeploy"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">

  

其中,appBase指定了虚拟目录所在位置.

2.在/tomcat6/conf/下创建与"Engine"标签name属于指定值一致的目录名

   比如我使用的是Catalina,那么就创建名为Catalina的目录,其下创建名为localhost(与Host标签的name属性一致)的目录 .

3.在我们的应用下,在目录META-INF添加ROOT.xml,内容为:

<?xml version="1.0" encoding="UTF-8"?>
< Context path="" docBase="ROOT" debug="0" reloadable="true"></Context>

 

 整个配置完成,重启服务器.

tomcat官方对Engine标签的说明:

AttributeDescriptionbackgroundProcessorDelay

This value represents the delay in seconds between the invocation of the backgroundProcess method on this engine and its child containers, including all hosts and contexts. Child containers will not be invoked if their delay value is not negative (which would mean they are using their own processing thread). Setting this to a positive value will cause a thread to be spawn. After waiting the specified amount of time, the thread will invoke the backgroundProcess method on this engine and all its child containers. If not specified, the default value for this attribute is 10, which represent a 10 seconds delay.

className

Java class name of the implementation to use. This class must implement the org.apache.catalina.Engine interface. If not specified, the standard value (defined below) will be used.

defaultHost

The default host name, which identifies the Host that will process requests directed to host names on this server, but which are not configured in this configuration file. This name MUST match thename attributes of one of the Host elements nested immediately inside.

jvmRoute

Identifier which must be used in load balancing scenarios to enable session affinity. The identifier, which must be unique across all Tomcat 6 servers which participate in the cluster, will be appended to the generated session identifier, therefore allowing the front end proxy to always forward a particular session to the same Tomcat 6 instance.

name

Logical name of this Engine, used in log and error messages. When using mulipleService elements in the same Server, each Engine MUST be assigned a unique name.

 

tomcat 官方对Host标签的说明:

AttributeDescriptionappBase

The Application Base directory for this virtual host. This is the pathname of a directory that may contain web applications to be deployed on this virtual host. You may specify an absolute pathname for this directory, or a pathname that is relative to the $CATALINA_BASE directory. See Automatic Application Deployment for more information on automatic recognition and deployment of web applications to be deployed automatically.

autoDeploy

This flag value indicates if new web applications, dropped in to the appBase directory while Tomcat is running, should be automatically deployed. The flag's value defaults to true. SeeAutomatic Application Deployment for more information.

backgroundProcessorDelay

This value represents the delay in seconds between the invocation of the backgroundProcess method on this host and its child containers, including all contexts. Child containers will not be invoked if their delay value is not negative (which would mean they are using their own processing thread). Setting this to a positive value will cause a thread to be spawn. After waiting the specified amount of time, the thread will invoke the backgroundProcess method on this host and all its child containers. A host will use background processing to perform live web application deployment related tasks. If not specified, the default value for this attribute is -1, which means the host will rely on the background processing thread of its parent engine.

className

Java class name of the implementation to use. This class must implement the org.apache.catalina.Host interface. If not specified, the standard value (defined below) will be used.

deployOnStartup

This flag value indicates if web applications from this host should be automatically deployed by the host configurator. The flag's value defaults to true. SeeAutomatic Application Deployment for more information.

name

Network name of this virtual host, as registered in your Domain Name Service server. One of the Hosts nested within anEngine MUST have a name that matches the defaultHost setting for that Engine. SeeHost Name Aliases for information on how to assign more than one network name to the same virtual host.

 

 最后,便可以通过http://localhost/进行访问了.

如果要添加其它应用,则将应用部署在appBase指定的目录,记得在应用的META-INF目录中添加"应用名.xml"就可以了.

当然,如:faceye.xml.那么,将来的访问就为:http://localhost/faceye.



<Tomcat安装目录>/conf文件夹下的server.xml文件中,<host>元素代表虚拟主机,在同一个<engine>元素下可以有多个细腻主机。

在未设置虚拟主机的默认情况下,server.xml配置文件中默认已经存在一个name属性为localhost的<host>元素。要配置新的主机只用在默认<host></host>元素后,新增一对<host></host>元素。

一对<host>元素中可以包含多个<alias>元素,<alias>元素表示为该主机指定别名。

例如:

<host name="www.eugeneheen.com"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
            <alias>eugeneheen.com</alias>
            <alias>eugeneheen</alias>
</host>

<host>元素的name属性的值代表虚拟主机名。

该虚拟主机name属性为www.eugeneheen.com,这时就能通过http://www.eugeneheen.com:8080/webapps/hello/hello.html来访问hello应用下的hello.html静态网页或其它资源。

appBase表明该应用属于文件的相对路径为/webapps目录下。(也可以是绝对路径)

例如:当前appBase属性为webapps,那么开放式发布就将整个项目结构复制到webapps目录下,如果事WAR打包文件,在服务器启动时就会自动解压一个名字与WAR包相同的项目结构文件夹到webapps下。(换言之appBase属性的值就应用发布的根目录)

unpackWARs属性如果为true就表明在appBase下的应用如果是打包好的WAR文件,则会先打开WAR包再运行,如果值为false就会直接运行WAR包。

autoDeploy属性的值为布尔类型,如果为true,则Tomcat在运行状态时,能够监测到appBase下的文件,如果有新的应用加入则会自动发布这个应用。

<host>元素内嵌套的<alias>元素能为该虚拟主机设置别名。可以设置多个别名。

例如:上面为虚拟主机www.eugeneheen.com设置了eugeneheen.com和eugeneheen两个别名。

这时我们除了能通过http://www.eugeneheen.com:8080/webapps/hello/hello.html来访问hello应用下的hello.html静态网页或其它资源外,还能通过:

http://eugeneheen.com:8080/webapps/hello/hello.html

http://eugeneheen:8080/webapps/hello/hello.html

这个来完成访问。

注意:如果要使配置的虚拟机生效,必须在DNS服务器上注册以上虚拟机名和别名。使它的IP地址都指向Tomcat服务器所在的主机。

必须注册<host>元素的name属性值和所有嵌套在<host>元素内的别名。

配置和注册完成后重启Tomcat服务器后就能正常使用虚拟主机和所有别名来进行访问了!

 

DNS服务器中注册虚拟主机和别名,使它们的IP地址指向Tomcat服务器所在主机:

以Windows XP为例,更改C:\WINDOWS\system32\drivers\etc\hosts文件中注册IP地址指向Tomcat服务器所在主机。

加入内容格式:IP +  空格 + host元素name属性/alias,例如:192.0.0.1 www.eugeneheen.com

加入后,完成DNS服务器注册,IP指向虚拟主机和别名。


0 0
原创粉丝点击