Tomcat(二) 配置文件 server.xml 详解

<?xml version='1.0' encoding='utf-8'?>  

<Server port="8005" shutdown="SHUTDOWN">  
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />  
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />  
  <Listener className="org.apache.catalina.core.JasperListener" />  
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />  
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />  
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />  

  <GlobalNamingResources>  

    <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>  

  <Service name="Catalina">     

    <!-- A "Connector" represents an endpoint by which requests are received  
         and responses are returned. Documentation at :  
         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)  
         Java AJP  Connector: /docs/config/ajp.html  
         APR (HTTP/AJP) Connector: /docs/apr.html  
         Define a non-SSL HTTP/1.1 Connector on port 8080  
    -->  
    <Connector port="8080" 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 8443  
         This connector uses the BIO implementation that requires the JSSE  
         style configuration. When using the APR/native implementation, the  
         OpenSSL style configuration is required as described in the APR/native  
         documentation -->  
    <!--  
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"  
               maxThreads="150" SSLEnabled="true" 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 processes  
         every request.  The Engine implementation for Tomcat stand alone  
         analyzes the HTTP headers included with the request, and passes them  
         on 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="Catalina" defaultHost="localhost" jvmRoute="jvm1">  
    -->  
    <Engine name="Catalina" defaultHost="localhost">  

      <!--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"/> 
      -->  

      <!-- Use the LockOutRealm to prevent attempts to guess user passwords  
           via a brute-force attack -->  
      <Realm className="org.apache.catalina.realm.LockOutRealm">  
        <!-- This Realm uses the UserDatabase configured in the global JNDI  
             resources under the key "UserDatabase".  Any edits  
             that are performed against this UserDatabase are immediately  
             available for use by the Realm.  -->  
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"  
               resourceName="UserDatabase"/>  
      </Realm>  

      <Host name="localhost"  appBase="webapps"  
            unpackWARs="true" autoDeploy="true">  

        <!-- SingleSignOn valve, share authentication between web applications  
             Documentation at: /docs/config/valve.html -->  
        <!-- 
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" /> 
        -->  

        <!-- Access log processes all example.  
             Documentation at: /docs/config/valve.html  
             Note: The pattern used is equivalent to using pattern="common" -->  
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"  
               prefix="localhost_access_log." suffix=".txt"  
               pattern="%h %l %u %t "%r" %s %b" />  

      </Host>  
    </Engine>  
  </Service>  
</Server>  

配置详解:这里写图片描述

Tomcat的配置层次:

<server>
    <service>
        <connector  />
        <engine>
            <host>
                <context></context>
            </host>
        </engine>
    </service>
</server>

组件分类:
顶级组件:位于整个配置的顶层;
容器类组件:可以包含其他的组件的组件;
连接器组件:连接用户请求至tomcat
被嵌套类的组件:位于一个容器当中,不能包含其他组件

容器类:
engine: 核心 catalina 引擎,负责通过connector接收用户请求
host:类似于httpd中的虚拟主机:支持基于FQDN的虚拟机
context:定义应用程序的部署,最内层的容器类组件,一个context代表一个web 应用程序;配置context的主要目的是指定对应的webapp 的跟目录;还能为webapp 指定额外的属性,如部署方式;

服务:
service:负责将连接器关联至engine;
因为一个service 内部可以有多个connector ,但只能有一个engine;
顶级组件:server 表示一个运行于JVM中的tomcat 实例;

嵌套类组件:
valve : 拦截请求并在将其转至对应的webapp 之前进行某种处理操作;可以用于任何容器中
access log value;
remote address filter value :基于IP做的访问控制
logger: 日志记录器,用于记录组件内部的状态信息;
可用于除context之外的任何容器中
realm 可以用于任何容器类的组件中,关联一个用户认证库,实现认证和授权;
UserDatabaseRealm: 使用JNDI自定义的用户认证
MemoryRealm:tomcat-users.xml中
JDBCRealm:基于JDBC连接至数据库中查找用户。

apache jserv protocal:
二进制协议,使用httpd 反向代理用户请求至tomcat 时,在httpd 和tomcat 之间使用

Server元素

它代表整个容器,是Tomcat实例的顶层元素.由org.apache.catalina.Server接口来定义.它包含一个
元素.并且它不能做为任何元素的子元素.

< Server port ="8005" shutdown ="SHUTDOWN" debug ="0" >

1>className指定实现org.apache.catalina.Server接口的类.默认值为
org.apache.catalina.core.StandardServer
2>port指定Tomcat监听shutdown命令端口.终止服务器运行时,必须在Tomcat服务器所在的机器上发出
shutdown命令.该属性是必须的.
3>shutdown指定终止Tomcat服务器运行时,发给Tomcat服务器的shutdown监听端口的字符串.该属性必须设

Serveice元素

该元素由org.apache.catalina.Service接口定义,它包含一个元素,以及一个或多个,这些Connector元素共享用同一个Engine元素

< Service name ="Catalina" >  
< Service name ="Apache" >  

第一个处理所有直接由Tomcat服务器接收的web客户请求.
第二个处理所有由Apahce服务器转发过来的Web客户请求
1>className 指定实现org.apahce.catalina.Service接口的类.默认为org.apahce.catalina.core.StandardService
2>name定义Service的名字

扫描二维码关注公众号,回复: 10491477 查看本文章

Host元素

它由Host接口定义.一个Engine元素可以包含多个元素.每个的元素定义了一个虚拟主机.它包含了一个或多个Web应用.

< Host name ="localhost" debug ="0" appBase ="webapps" unpackWARs ="true" autoDeploy ="true" >
lassName指定实现Host接口的类.默认值为StandardHost
2>appBase指定虚拟主机的目录,可以指定绝对目录,也可以指定相对于的相对目录.如果没有此项,
默认为/webapps
3>autoDeploy如果此项设为true,表示Tomcat服务处于运行状态时,能够监测appBase下的文件,如果有新有web应用加入进来,会自运发布这个WEB应用
4>unpackWARs如果此项设置为true,表示把WEB应用的WAR文件先展开为开放目录结构后再运行.如果设为false将直接运行为WAR文件
5>alias指定主机别名,可以指定多个别名
6>deployOnStartup如果此项设为true,表示Tomcat服务器启动时会自动发布appBase目录下所有的Web应用
如果Web应用中的server.xml没有相应的元素,将采用Tomcat默认的Context
7>name定义虚拟主机的名字在元素中可以包含如下子元素, , ,

Context元素

它由Context接口定义.是使用最频繁的元素.每个

Connector 元素

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

< Connector port ="8080" maxThread ="50" minSpareThreads ="25" maxSpareThread ="75" enableLookups ="false" redirectPort ="8443"   
acceptCount ="100" debug ="0" connectionTimeout ="20000" disableUploadTimeout ="true" />  
t; Connection port ="8009" enableLookups ="false" redirectPort ="8443" debug ="0" protocol ="AJP/1.3" />  

第一个Connector元素定义了一个HTTP Connector,它通过8080端口接收HTTP请求;第二个Connector元素定义了一个JD Connector,它通过8009端口接收由其它服务器转发过来的请求.
Connector元素共用属性
1>className指定实现Connector接口的类
2>enableLookups如果设为true,表示支持域名解析,可以把IP地址解析为主机名.WEB应用中调用request.getRemoteHost方法,
返回客户机主机名.默认值为true
3>redirectPort指定转发端口.如果当前端口只支持non-SSL请求,在需要安全通信的场命,将把客户请求转发至SSL的redirectPort端口HttpConnector元素的属性
1>className实现Connector的类
2>port设定Tcp/IP端口,默认值为8080,如果把8080改成80,则只要输入http://localhost 即可因为TCP/IP的默认端口是80
3>address如果服务器有二个以上ip地址,此属性可以设定端口监听的ip地址.默认情况下,端口会监听服务器上所有的ip地址
4>bufferSize设定由端口创建的输入流的缓存大小.默认值为2048byte
5>protocol设定Http协议,默认值为HTTP/1.1
6>maxThreads设定在监听端口的线程的最大数目,这个值也决定了服务器可以同时响应客户请求的最大数目.默认值为200
7>acceptCount设定在监听端口队列的最大客户请求数量,默认值为10.如果队列已满,客户必须等待.
8>connectionTimeout定义建立客户连接超时的时间.如果为-1,表示不限制建立客户连接的时间

JkConnector的属性

1>className实现Connector的类
2>port设定AJP端口号
3>protocol必须设定为AJP/1.3

发布了23 篇原创文章 · 获赞 23 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/xiaojiang_520/article/details/79776901