Tomcat配置建立多站点、多域名、多端口、多应用、多主机汇总大全

Tomcat配置建立多站点、多域名、多端口、多应用、多主机汇总大全

当你使用Tomcat作为Web Server的时候,是不是会想过这样的一个问题:如何利用Tomcat建立多个Web应用 呢?

要实现这一点是很简单的,也有多种方法.(以下说明使用%tomcat_home%代表Tomcat安装目录).

一.首先介绍一下Tomcat及server.xml

Tomcat服务器是由一系列的可配置的组件构成,tomcat的组件可以

在%tomcat_home%/conf/server.xml文件中进行配置,每个Tomcat组件和server.xml文件的一种配置元素对应.

主要分为4类:

1.顶层类元素:包括和,他们位于整个配置文件的顶层.

元素代表整个Catalina Servlet 容器,由org.apache.catalin.Server接口定义.包含一个或多个元素.

元素由org.apache.catalin.Service 接口定义.包含一个元素,及一个或多个元素.多个元素共享一个元素.

2.连接器类元素

连接器类代表了介于客户与服务之间的通信接口,负责将客户的请求发送给服务器,并将服务器的响应结果传递给客户.

元素由org.apache.catalin.Connector 接口定义.代表了与客户程序实际交互的组件,它负责接收客户请求,以及向客户返回响应结果.

3.容器类元素

容器类元素代表处理客户请求并生成响应的组件.

元素由org.apache.catalin.Engine 接口定义.每个只能包含一个元素,元素处理在同一个中的所有元素收到的客户请求.

元素由org.apache.catalin.Host 接口定义.一个元素中可以包含多个元素.每个元素定义了一个虚拟主机,她可以包含一个或多个Web 应用.

元素由org.apache.catalin.Context 接口定义.代表了运行在虚拟主机上的一个Web 应用.一个元素可以包含多个元素

4.嵌套类元素

嵌套类元素代表了可以加到容器中的组件,

关于server.xml的更多信息,可以参考Tomcat的文

档:/webapps/tomcat-docs/config/index.html

样例:

<Server>

<Service name="Catalina">

<Connector acceptCount="100" connectionTimeout="20000"

disableUploadTimeout="true" port="8080" redirectPort="8443"

maxSpareThreads="75" maxThreads="150" minSpareThreads="25"/> <Connector port="8009" protocol="AJP/1.3"

protocolHandlerClassName="org.apache.jk.server.JkCoyoteHandler" redirectPort="8443"/>

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

<Host appBase="webapps" name="localhost">

<Logger className="org.apache.catalina.logger.FileLogger" prefix="localhost_log." suffix=".txt" timestamp="true"/>

</Host>

<Logger className="org.apache.catalina.logger.FileLogger" prefix="catalina_log." suffix=".txt" timestamp="true"/>

<Realm className="org.apache.catalina.realm.UserDatabaseRealm"/> </Engine>

</Service>

</Server>

二.建立多个Web应用方法:

在这之前,我已经将默认8080更改为80了.

1.通过配置多个元素(这是最为普遍的方法)

在下配置多个元素

<Context path="webapps" docBase="C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\ROOT" debug="0"

reloadable="true"></Context>

<Context path="webapps2" docBase="C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps2\ROOT" debug="0"

reloadable="true"></Context>

后通过 主机名:端口/应用名 访问,如: http://localhost/app1 或 http://localhost/app2

2.通过配置多个元素

在下配置多个元素

<Host appBase="webapps" name="xok.la">

<Alias>www.xok.la</Alias>

<Context path="" docBase="C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\ROOT" debug="0"

reloadable="true"></Context>

</Host>

<Host appBase="webapps2" name="xok.cc">

<Alias>www.xok.cc</Alias>

<Context path="" docBase="C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps2\ROOT" debug="0"

reloadable="true"></Context>

</Host>

然后通过 主机名:端口 访问,如: http://xok.la 或 http://xok.cc 需要注意的是这样需要机器连接到局域网上.

3.通过配置多个元素(多端口 多应用)

在下配置多个元素

<Service name="Catalina">

<Connector acceptCount="100" connectionTimeout="20000"

disableUploadTimeout="true" port="80" redirectPort="8453"

maxSpareThreads="75" maxThreads="150" minSpareThreads="25"/> <Connector port="8019" protocol="AJP/1.3"

protocolHandlerClassName="org.apache.jk.server.JkCoyoteHandler" redirectPort="8453"/>

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

<Host appBase="webapps" name="localhost">

<Context path="" docBase="C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\ROOT" debug="0"

reloadable="true"></Context>

</Host>

<Realm className="org.apache.catalina.realm.UserDatabaseRealm"/> </Engine>

</Service>

<Service name="Catalina2">

<Connector acceptCount="100" connectionTimeout="20000"

disableUploadTimeout="true" port="90" redirectPort="9443"

maxSpareThreads="75" maxThreads="150" minSpareThreads="25"/>

<Connector port="9009" protocol="AJP/1.3"

protocolHandlerClassName="org.apache.jk.server.JkCoyoteHandler" redirectPort="9443"/>

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

<Host appBase="webapps" name="localhost">

<Context path="" docBase="C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps2\ROOT" debug="0"

reloadable="true"></Context>

</Host>

<Realm className="org.apache.catalina.realm.UserDatabaseRealm"/> </Engine>

</Service>

第2个应用只是在默认端口第1位数字上加1

定义了两个Service分别是Catalina和Catalina2,侦听的端口分别是80和90 然后通过 主机名:端口 访问,如: http://localhost:80 或

http://localhost:90

 

第二篇:tomcat域名配置

TOMCAT域名配置2010-10-19 17:28现在很多的公司的网站都是用tomcat作为应用服务区的,可是对于初学者,8080端口号是如何去掉的,这些网站是如何和域名绑定到一起的呢?一个 tomcat是如何绑定多域名?并且这些域名是如何对应不同的项目呢?而且我的服务器没放在机房,放在我的本地,我应该如何把我的服务器给映射到外网?这一系列的问题,今天就这个问题,我来说一下。 1. 域名。现在的域名不是很贵,有很多人都有,而我们注册域名的网站也会有一个DNS助解析,就是把服务器的IP绑定到这些域名上。 2. 我们域名有了,现在就来说说我们的项目;比如说我现在有两个项目都在我的这个tomcat下面。那我们输入http://localhost:8080 /abc就可以访问到我们abc这个项目了,但是端口号8080如何去掉呢?在tomcat的conf文件夹下有一个server.xml。我们打开这个文件,找到8080,只需要把8080改成80即可。当我们再次输入http://localhost/abc即可。 3. 可是我们的域名如何跟项目绑定到一起呢?我们再次找到tomcat下面的conf文件夹里面的server.xml。修改<Host name="我们所要绑定的域名" debug="0" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"></Host> 4. 当启动tomcat的时候,输入我们绑定的域名,是不是看到了我们的tomcat。可是我们想访问我们的项目还得再域名后面加上项目名称才能访问到,也就是说到现在我们的域名还没跟项目绑定到一起。我们在看一下第三部,想让我们的项目和域名绑定到一起,我们只需要在</Host>前面加上<Context docBase="项目名" path="" reloadable="true" >即可。 例如:<Host name="" debug="0" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"><Context docBase="baidu" path="" reloadable="true" ></Host> 5. 当我们的有两个项目都放在同一个tomcat下的时候,我们只需要把<Host></ Host>加上两个既可以完成我们的2个项目的绑定,但是项目名称别忘了改哦。 6. 当我们的服务器自己维护(没有放在机房),我们需要对我们的路由做一下映射。就拿我的路由来举例。我的路由是TP-LINK WR340G型号,进入路由后,我们能找到一个转发规则,那里面有一个虚拟服务器的这项,点击新增条目以后会有:服务端口号、IP地址、协议、状态、常用服务端口号。我们只需在这里IP地址(服务器的局域网IP),协议选择ALL(有的路由好像没有这项,那就不需要填写了),状态选择生效,常用服务端口号选择HTTP或者WEB即可。这时默认的端口号为80。当常用服务端口号没有HTTP或者WEB这项时,可以选择DNS,然后手动把端口号改成80。点击生效。保存即

可。如果说本人说的路由映射对您不适用或者没有看明白,可以上网找各种路由的映射方法。 7. 这个时候我们在输入我们的域名,是不是就直接访问到我们的项目了。转载某某的……Tomcat配置一个ip绑定多个域名2009-07-02 11:36本人对tomcat下配置 一ip对多域名的方法详细如下,按下面配置一定能成功,经过测试了.<Host name="localhost" debug="0" appBase="D:\\Tomcat 5.0\\webapps"unpackWARs="true" autoDeploy="true"xmlValidation="false" xmlNamespaceAware="false"><Alias>localhost.com</Alias> <Logger className="org.apache.catalina.logger.FileLogger"directory="logs" prefix="localhost_log." suffix=".txt"timestamp="true"/><Context path="" docBase="D:\\Tomcat 5.0\\webapps\\gg" reloadable="true" caseSensitive="false" debug="0"></Context></Host><Host name="one.localhost" debug="0" appBase="D:\\Tomcat 5.0\\webapps"unpackWARs="true" autoDeploy="true"xmlValidation="false" xmlNamespaceAware="false"><Alias></Alias> <Logger className="org.apache.catalina.logger.FileLogger"directory="logs" prefix="localhost_log." suffix=".txt"timestamp="true"/><Context path="" docBase="D:\\Tomcat 5.0\\webapps\\kk\\a" reloadable="true" caseSensitive="false" debug="0"></Context></Host><Host name="two.localhost" debug="0" appBase="D:\\Tomcat 5.0\\webapps"unpackWARs="true" autoDeploy="true"xmlValidation="false" xmlNamespaceAware="false"><Alias></Alias> <Logger className="org.apache.catalina.logger.FileLogger"directory="logs" prefix="localhost_log." suffix=".txt"timestamp="true"/><Context path="" docBase="D:\\Tomcat 5.0\\webapps\\kk\\b" reloadable="true" caseSensitive="false" debug="0"></Context></Host>因无域名解析服务器,所以在hosts上配,来模拟dns解析.10.1.1.241 localhost 指向gg文件夹10.1.1.241 localhost.com 指向gg文件夹10.1.1.241 one.localhost 指向a文件夹10.1.1.241 指向a文件夹10.1.1.241 two.localhost 指向b文件夹10.1.1.241 指向b文件夹经过多方面尝试,终于在tomcat下配置成功了.修改默认的应用时:改defaulthost,并将context的path置为空。配置多个应用时:如:http://localhost:8080/http://localhost:8080/firsthttp://localhost:8080/second对应的配置如下:<Host name="localhost" debug="0" appBase="D:\\Tomcat 5.0\\webapps"unpackWARs="true" autoDeploy="true"xmlValidation="false" xmlNamespaceAware="false"><Alias>localhost.com</Alias> <Logger className="org.apache.catalina.logger.FileLogger"directory="logs" prefix="localhost_log.

" suffix=".txt"timestamp="true"/><Context path="" docBase="D:\\Tomcat 5.0\\webapps\\Root" reloadable="true" caseSensitive="false" debug="0"></Context><Context path="/first" docBase="D:\\Tomcat 5.0\\webapps\\first" reloadable="true" caseSensitive="false" debug="0"></Context><Context path="/second" docBase="D:\\Tomcat 5.0\\webapps\\second" reloadable="true" caseSensitive="false" debug="0"></Context></Host>tomcat下servlet.xml文件配置域名20xx年04月15日 星期四 下午 03:10<?xml version='1.0' encoding='utf-8'?><!--Licensed to the Apache Software Foundation (ASF) under one or morecontributor license agreements. See the NOTICE file distributed withthis work for additional information regarding copyright ownership.The ASF licenses this file to You under the Apache License, Version 2.0(the "License"); you may not use this file except in compliance withthe License. You may obtain a copy of the License at/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.--><!-- Note: A "Server" is not itself a "Container", so you may notdefine subcomponents such as "Valves" at this level.Documentation at /docs/config/server.html--><Server port="8005" shutdown="SHUTDOWN"><!--APR library loader. Documentation at /docs/apr.html --><Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /><!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html --><Listener className="org.apache.catalina.core.JasperListener" /><!-- JMX Support for the Tomcat server. Documentation at /docs/non-existent.html --><Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" /><Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /><!-- Global JNDI resourcesDocumentation at /docs/jndi-resources-howto.html--><GlobalNamingResources><!-- Editable user database that can also be used byUserDatabaseRealm to authenticate users--><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" /></GlobalNamingResources><!-- A "Service" is a collection of one or more "Connectors" that sharea single "Container" Note: A "Service" is not itself a "Container", so you may not define subcomponents such as "Valves" at this level.Documentation at /docs/config/service.html

--><Service name="Catalina"><!--The connectors can use a shared executor, you can define one or more named thread pools--><!--<Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="150" minSpareThreads="4"/>--><!-- A "Connector" represents an endpoint by which requests are receivedand responses are returned. Documentation at :Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)Java AJP Connector: /docs/config/ajp.htmlAPR (HTTP/AJP) Connector: /docs/apr.htmlDefine a non-SSL HTTP/1.1 Connector on port 8080--><Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /><!-- A "Connector" using the shared thread pool--><!--<Connector executor="tomcatThreadPool"port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />--> <!-- Define a SSL HTTP/1.1 Connector on port 8443This connector uses the JSSE configuration, when using APR, the connector should be using the OpenSSL style configurationdescribed in the APR documentation --><!--<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"maxThreads="150" scheme="https" secure="true"clientAuth="false" sslProtocol="TLS" />--><!-- Define an AJP 1.3 Connector on port 8009 --><Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /><!-- An Engine represents the entry point (within Catalina) that processesevery request. The Engine implementation for Tomcat stand aloneanalyzes the HTTP headers included with the request, and passes themon to the appropriate Host (virtual host).Documentation at /docs/config/engine.html --><!-- You should set jvmRoute to support load-balancing via AJP ie :<Engine name="Standalone" defaultHost="localhost" jvmRoute="jvm1"> --> <Engine name="Catalina" defaultHost=""><!--For clustering, please take a look at documentation at:/docs/cluster-howto.html (simple how to)/docs/config/cluster.html (reference documentation) --><!--<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>--> <!-- The request dumper valve dumps useful debugging information aboutthe request and response data received and sent by Tomcat.Documentation at: /docs/config/valve.html --><!--<Valve className="org.apache.catalina.valves.RequestDumperValve"/>--><!-- This Realm uses the UserDatabase configured in the global JNDIresources under the key "UserDatabase". Any editsthat are performed against this UserDat

abase are immediatelyavailable for use by the Realm. --><Realm className="org.apache.catalina.realm.UserDatabaseRealm"resourceName="UserDatabase"/><!-- Define the default virtual hostNote: XML Schema validation will not work with Xerces 2.2.--><Host name="" appBase="webapps"unpackWARs="true" autoDeploy="true"xmlValidation="false" xmlNamespaceAware="false"><Alias></Alias> <Alias></Alias> <Alias>meemei.com</Alias> <Context path="" docBase="D:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\meemei" debug="0"/><!-- SingleSignOn valve, share authentication between web applicationsDocumentation at: /docs/config/valve.html --><!--<Valve className="org.apache.catalina.authenticator.SingleSignOn" />--><!-- Access log processes all example.Documentation at: /docs/config/valve.html --><!--<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/>--></Host></Engine></Service></Server>tomcat配置虚拟目录和域名2009-06-03 16:11在$tomcat/webapps/下建了个myjsp目录作为我网站的默认目录,在myjsp中有一个a.jsp文件,该文件要作为我网站的默认主页。 修改配置文件: 首先,修改$tomcat/conf/server.xml文件。 在server.xml文件中,有一段如下: …… <engine name="Catalina" defaultHost="localhost改为你的域名"> <host name="localhost改为你的域名" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> …… <host> </engine> …… 在<host></host>标签之间添加上: <Context path="" docBase="myjsp" debug="0" reloadable="true" /> path是说明虚拟目录的名字,如果你要只输入ip地址就显示主页,则该键值留为空; docBase是虚拟目录的路径,它默认的是$tomcat/webapps/ROOT目录,现在我在webapps目录下建了一个myjsp目录,让该目录作为我的默认目录。 debug和reloadable一般都分别设置成0和true。 更改端口 <Connector port="8080改为80或者你想要的端口" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" debug="0" connectionTimeout="20000" disableUploadTimeout="true" /> 将port "8080"改成你的端口,web服务器一般为80端口,你就改为80 然后,修改$tomcat/conf/web.xml文件。 在web.xml文件中,有一段如下: <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file>

<welcome-file>index.jsp</welcome-file> </welcome-file-list> 在<welcome-file-list>与<welcome-file>index.html</welcome-file>之间添加上: <welcome-file>a.jsp</welcome-file> 下面这段比较重要 〈init-param〉 〈param-name〉listings〈/param-name〉 〈param-value〉false(一定要改为false,否则输入域名不能访问)〈/param-value〉 〈/init-param〉 保存上述两个文件后重启tomcat,在浏览器地址栏内输入"http://你的域名",显示a.jsp页面的内容。

相关推荐