详解Tomcat主配置文件server.xml

目录
文章目录隐藏
  1. 1. server.xml 组件类别
  2. 2. 组件介绍
  3. 3. server.xml 配置文件详解
  4. 4 主要参数详解

Tomcat 主配置文件 server.xml 是 Tomcat 服务器的主要配置文件,文件位置在 conf 目录下,它包含了 Tomcat 的全局配置信息,包括监听端口、虚拟主机、安全配置、连接器等。

1. server.xml 组件类别

顶级组件:位于整个配置的顶层。如:server。

  • 容器类组件:可以包含其他组件的组件。如:service、engine、host、context。
  • 连接器组件:连接用户请求至 Tomcat。如:connector。
  • 被嵌套类组件:位于一个容器中,不能包含其他组件。如 value、logger。

2. 组件介绍

组件名称 功能介绍
engine 核心容器组件,定义 Tomcat 服务器内部的容器,与 Service 元素一起定义了 Tomcat 服务器的整体架构。catalina 引擎,负责通过 connector 接收用户请求,并处理请求,将请求转至对应的虚拟主机 host。
host 类似于 httpd 中的虚拟主机,一般而言支持基于 FQDN 的虚拟主机,允许在同一台服务器上运行多个网站或应用程序。
context 定义一个应用程序的上下文,包括 Web 应用程序的路径、名称、文档根目录等,是一个最内层的容器类组件(不能再嵌套)。配置 context 的主要目的指定对应的 webapp 的根目录,类似于 httpd 的 alias,其还能为 webapp 指定额外的属性,如部署方式等。
connector 定义 Tomcat 服务器与外部应用程序或客户端之间的连接,接收用户请求,通常用于 HTTP 或 HTTPS 通讯,类似于 httpd 的 listen 配置监听端口。
Service 定义 Tomcat 服务器提供的服务,通常包含一个或多个 Connector(连接器),但只能有一个引擎 engine。
Server 定义 Tomcat 服务器的全局属性,其中的 port 属性定义了 Tomcat 服务器本身监听的端口号。
Valve 通过提供不同类型的阀门,拦截请求并在将其转至对应的 webapp 前进行某种处理操作,可以用于任何容器中,比如记录日志(access log valve)、基于 IP 做访问控制(remote address filter valve),实现对 Tomcat 服务器的访问控制、流量控制、日志记录等功能。
loggor 日志记录器,用于记录组件内部的状态信息,可以用于除 context 外的任何容器中。
Realm 定义 Tomcat 服务器的安全认证和授权机制。可以用于任意容器类的组件中,关联一个用户认证库,实现认证和授权。可以关联的认证库有: UserDatabaseRealm(使用 JNDI 自定义的用户认证库)、MemoryRealm(认证信息定义在 tomcat-users.xml 中)和 JDBCRealm(认证信息定义在数据库中,并通过 JDBC 连接至数据库中查找认证用户)。

3. server.xml 配置文件详解

server.xml 文件原版:

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this 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 with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed 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 and
  limitations under the License.
-->
<!-- Note:  A "Server" is not itself a "Container", so you may not
     define subcomponents such as "Valves" at this level.
     Documentation at /docs/config/server.html
 -->
<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <!-- Security listener. Documentation at /docs/config/listeners.html
  <Listener className="org.apache.catalina.security.SecurityListener" />
  -->
  <!-- APR library loader. Documentation at /docs/apr.html -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

  <!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm 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 share
       a 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 received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
    -->
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"
               maxParameterCount="1000"
               />
    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"
               maxParameterCount="1000"
               />
    -->
    <!-- Define an SSL/TLS HTTP/1.1 Connector on port 8443
         This connector uses the NIO implementation. The default
         SSLImplementation will depend on the presence of the APR/native
         library and the useOpenSSL attribute of the AprLifecycleListener.
         Either JSSE or OpenSSL style configuration may be used regardless of
         the SSLImplementation selected. JSSE style configuration is used below.
    -->
    <!--
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
               maxThreads="150" SSLEnabled="true"
               maxParameterCount="1000"
               >
        <SSLHostConfig>
            <Certificate certificateKeystoreFile="conf/localhost-rsa.jks"
                         type="RSA" />
        </SSLHostConfig>
    </Connector>
    -->
    <!-- Define an SSL/TLS HTTP/1.1 Connector on port 8443 with HTTP/2
         This connector uses the APR/native implementation which always uses
         OpenSSL for TLS.
         Either JSSE or OpenSSL style configuration may be used. OpenSSL style
         configuration is used below.
    -->
    <!--
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol"
               maxThreads="150" SSLEnabled="true"
               maxParameterCount="1000"
               >
        <UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
        <SSLHostConfig>
            <Certificate certificateKeyFile="conf/localhost-rsa-key.pem"
                         certificateFile="conf/localhost-rsa-cert.pem"
                         certificateChainFile="conf/localhost-rsa-chain.pem"
                         type="RSA" />
        </SSLHostConfig>
    </Connector>
    -->

    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <!--
    <Connector protocol="AJP/1.3"
               address="::1"
               port="8009"
               redirectPort="8443"
               maxParameterCount="1000"
               />
    -->

    <!-- 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 &quot;%r&quot; %s %b" />

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

将原文件中英文注释行删掉并添加了中文详解注释。

<?xml version="1.0" encoding="UTF-8"?>

<Server> 元素代表整个容器,是 Tomcat 实例的顶层元素,由 org.apache.catalina.Server 接口来定义,它包含一个<Service>元素,并且它不能做为任何元素的子元素。
port 指定 Tomcat 监听 shutdown 命令端口,终止服务器运行时,必须在 Tomcat 服务器所在的机器上发出 shutdowm 命令,该属性是必须的,其端口号可以修改。
shutdown 指定终止 Tomcat 服务器运行时,发给 Tomcat 服务器的 shutdown 监听端口的字符串,该属性必须设置。
<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.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 服务组件:

<Service name="Catalina">

Connector 主要参数:

<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"
               maxParameterCount="1000"
               />

Engine 核心容器组件,catalina 引擎,负责通过 connector 接收用户请求,并处理请求,将请求转至对应的虚拟主机 host。

defaultHost 指定缺省的处理请求的主机名,它至少与其中的一个 host 元素的 name 属性值一样。

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

Realm 表示存放的用户名、密码及 role 的数据库。

<Realm className="org.apache.catalina.realm.LockOutRealm">
    <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
    resourceName="UserDatabase"/>
</Realm>

host 参数:

<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
    <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
        prefix="localhost_access_log" suffix=".txt"
        pattern="%h %l %u %t &quot;%r&quot; %s %b" />
 
</Host>

4 主要参数详解

4.1 Connector 主要参数详解

Connector 是 Tomcat 服务器与外部应用程序或客户端之间的连接,常见的 Connector 类型包括 HTTP、HTTPS、AJP 等。

Connector 主要参数详解

参数 参数说明
Connector 定义 Tomcat 服务器与外部应用程序或客户端之间的连接,接收用户请求,通常用于 HTTP 或 HTTPS 通讯,类似于 httpd 的 listen 配置监听端口。
port 指定 Connector 监听的端口号,用于监听来自客户端的请求。
protocol 连接器使用的协议,指定 Connector 要使用的协议类型,常见的有 HTTP/1.1、HTTP/2、AJP/1.3 等。
connectionTimeout 指定超时的时间数(以毫秒为单位),即在指定时间内未收到客户端请求,则连接被关闭。
redirectPort 指定重定向端口,即在使用 HTTPS 时,自动将 HTTP 请求重定向到 HTTPS。
maxParameterCount 最大可以创建的处理请求的线程数。

 4.2 host 参数详解

在 Tomcat 中,一个物理服务器可以部署多个虚拟主机,每个虚拟主机拥有自己的域名和独立的配置,这些虚拟主机通过 Host 元素来实现。

host 参数详解

参数 参数说明
host Server 元素的子元素,代表一个虚拟主机
name 虚拟主机的名称
appBase 指定该虚拟主机的 Web 应用程序的基础目录,Web 应用程序在该目录下部署。
unpackWARs 是否在部署 Web 应用程序时解压 WAR 文件,可以提高 Web 应用程序的访问速度。
autoDeploy 是否自动部署新的 Web 应用程序,如果设置为 true,则 Tomcat 会自动检测 appBase 目录下的新的 Web 应用程序,并进行自动部署。

 4.3 Context 参数说明

在 Tomcat 中,Context 参数是指一个 Web 应用程序的上下文信息,它包含了 Web 应用程序的配置信息、资源、Servlet 等。当一个 Web 应用程序被部署到 Tomcat 服务器上时,Tomcat 会为该 Web 应用程序创建一个 Context 对象,用于管理 Web 应用程序的运行时状态。

参数 参数说明
Context 表示一个 web 应用程序,通过为 war 文件。
docBase 表示 Web 应用程序的根目录,即 Web 应用程序的发布目录。应用程序的路径或者是 WAR 文件存放的路径,也可以使用相对路径,起始路径为此 Context 所属 Host 中 appBase 定义的路径。
path 表示 Web 应用程序的上下文路径,即访问该 Web 应用程序的 URL 路径。
reloadable 这个属性非常重要,如果为 true,则 tomcat 会白动检测应用程序的/WEB-INF/lib 和/WEB-INF/classes 目录的变化,自动装载新的应用程序,可以在不重启 tomcat 的情况下改变应用程序。
crossContext 用于指定不同的 Web 应用程序之间是否可以共享 ServletContext 对象。如果 crossContext 被设置为 true,则表示允许跨上下文共享 ServletContext 对象,否则不允许。

以上就是关于 Tomcat 主配置文件 server.xml 的内容,更多相关 Tomcat server.xml 内容请码云笔记的其他相关文章。

「点点赞赏,手留余香」

1

给作者打赏,鼓励TA抓紧创作!

微信微信 支付宝支付宝

还没有人赞赏,快来当第一个赞赏的人吧!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系maynote@foxmail.com处理
码云笔记 » 详解Tomcat主配置文件server.xml

发表回复