Tomcat主配置文件server.xml詳解
前言
Tomcat主配置文件(server.xml)是Tomcat服務(wù)器的主要配置文件,文件位置在conf目錄下,它包含了Tomcat的全局配置信息,包括監(jiān)聽端口、虛擬主機、安全配置、連接器等。
1 server.xml組件類別
頂級組件:位于整個配置的頂層。如:server。
- 容器類組件:可以包含其他組件的組件。如:service、engine、host、context。
- 連接器組件:連接用戶請求至Tomcat。如:connector。
- 被嵌套類組件:位于一個容器中,不能包含其他組件。如value、logger。
2 組件介紹
組件名稱 | 功能介紹 |
engine | 核心容器組件,定義Tomcat服務(wù)器內(nèi)部的容器,與Service元素一起定義了Tomcat服務(wù)器的整體架構(gòu)。catalina引擎,負(fù)責(zé)通過connector接收用戶請求,并處理請求,將請求轉(zhuǎn)至對應(yīng)的虛擬主機host。 |
host | 類似于httpd中的虛擬主機,一般而言支持基于FQDN的虛擬主機,允許在同一臺服務(wù)器上運行多個網(wǎng)站或應(yīng)用程序。 |
context | 定義一個應(yīng)用程序的上下文,包括Web應(yīng)用程序的路徑、名稱、文檔根目錄等,是一個最內(nèi)層的容器類組件(不能再嵌套)。配置context的主要目的指定對應(yīng)的webapp的根目錄,類似于httpd的alias,其還能為webapp指定額外的屬性,如部署方式等。 |
connector | 定義Tomcat服務(wù)器與外部應(yīng)用程序或客戶端之間的連接,接收用戶請求,通常用于HTTP或HTTPS通訊,類似于httpd的listen配置監(jiān)聽端口。 |
Service | 定義Tomcat服務(wù)器提供的服務(wù),通常包含一個或多個Connector(連接器),但只能有一個引擎engine。 |
Server | 定義Tomcat服務(wù)器的全局屬性,其中的port屬性定義了Tomcat服務(wù)器本身監(jiān)聽的端口號。 |
Valve | 通過提供不同類型的閥門,攔截請求并在將其轉(zhuǎn)至對應(yīng)的webapp前進(jìn)行某種處理操作,可以用于任何容器中,比如記錄日志(access log valve)、基于IP做訪問控制(remote address filter valve),實現(xiàn)對Tomcat服務(wù)器的訪問控制、流量控制、日志記錄等功能。 |
loggor | 日志記錄器,用于記錄組件內(nèi)部的狀態(tài)信息,可以用于除context外的任何容器中。 |
Realm | 定義Tomcat服務(wù)器的安全認(rèn)證和授權(quán)機制。可以用于任意容器類的組件中,關(guān)聯(lián)一個用戶認(rèn)證庫,實現(xiàn)認(rèn)證和授權(quán)??梢躁P(guān)聯(lián)的認(rèn)證庫有: UserDatabaseRealm(使用JNDI自定義的用戶認(rèn)證庫)、MemoryRealm(認(rèn)證信息定義在tomcat-users.xml中)和JDBCRealm(認(rèn)證信息定義在數(shù)據(jù)庫中,并通過JDBC連接至數(shù)據(jù)庫中查找認(rèn)證用戶)。 |
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 "%r" %s %b" /> </Host> </Engine> </Service> </Server>
將原文件中英文注釋行刪掉并添加了中文詳解注釋。
<?xml version="1.0" encoding="UTF-8"?> <Server> 元素代表整個容器,是Tomcat實例的頂層元素,由org.apache.catalina.Server接口來定義,它包含一個<Service>元素,并且它不能做為任何元素的子元素。 port 指定Tomcat監(jiān)聽shutdown命令端口,終止服務(wù)器運行時,必須在Tomcat服務(wù)器所在的機器上發(fā)出shutdowm命令,該屬性是必須的,其端口號可以修改。 shutdown 指定終止Tomcat服務(wù)器運行時,發(fā)給Tomcat服務(wù)器的shutdown監(jiān)聽端口的字符串,該屬性必須設(shè)置。 <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服務(wù)組件
<Service name="Catalina">
Connector主要參數(shù)
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" maxParameterCount="1000" />
Engine 核心容器組件,catalina引擎,負(fù)責(zé)通過connector接收用戶請求,并處理請求,將請求轉(zhuǎn)至對應(yīng)的虛擬主機host。
defaultHost 指定缺省的處理請求的主機名,它至少與其中的一個host元素的name屬性值一樣。
<Engine name="Catalina" defaultHost="localhost">
Realm 表示存放的用戶名、密碼及role的數(shù)據(jù)庫。
<Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm>
host參數(shù)
<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 "%r" %s %b" /> </Host> </Engine> </Service> </Server>
4 主要參數(shù)詳解
4.1 Connector主要參數(shù)詳解
Connector是Tomcat服務(wù)器與外部應(yīng)用程序或客戶端之間的連接,常見的Connector類型包括HTTP、HTTPS、AJP等。
參數(shù) | 參數(shù)說明 |
Connector | 定義Tomcat服務(wù)器與外部應(yīng)用程序或客戶端之間的連接,接收用戶請求,通常用于HTTP或HTTPS通訊,類似于httpd的listen配置監(jiān)聽端口。 |
port | 指定Connector監(jiān)聽的端口號,用于監(jiān)聽來自客戶端的請求。 |
protocol | 連接器使用的協(xié)議,指定Connector要使用的協(xié)議類型,常見的有HTTP/1.1、HTTP/2、AJP/1.3等。 |
connectionTimeout | 指定超時的時間數(shù)(以毫秒為單位),即在指定時間內(nèi)未收到客戶端請求,則連接被關(guān)閉。 |
redirectPort | 指定重定向端口,即在使用HTTPS時,自動將HTTP請求重定向到HTTPS。 |
maxParameterCount | 最大可以創(chuàng)建的處理請求的線程數(shù)。 |
4.2 host參數(shù)詳解
在Tomcat中,一個物理服務(wù)器可以部署多個虛擬主機,每個虛擬主機擁有自己的域名和獨立的配置,這些虛擬主機通過Host元素來實現(xiàn)。
參數(shù) | 參數(shù)說明 |
host | Server元素的子元素,代表一個虛擬主機 |
name | 虛擬主機的名稱 |
appBase | 指定該虛擬主機的Web應(yīng)用程序的基礎(chǔ)目錄,Web應(yīng)用程序在該目錄下部署。 |
unpackWARs | 是否在部署Web應(yīng)用程序時解壓WAR文件,可以提高Web應(yīng)用程序的訪問速度。 |
autoDeploy | 是否自動部署新的Web應(yīng)用程序,如果設(shè)置為true,則Tomcat會自動檢測appBase目錄下的新的Web應(yīng)用程序,并進(jìn)行自動部署。 |
4.3 Context參數(shù)說明
在Tomcat中,Context參數(shù)是指一個Web應(yīng)用程序的上下文信息,它包含了Web應(yīng)用程序的配置信息、資源、Servlet等。當(dāng)一個Web應(yīng)用程序被部署到Tomcat服務(wù)器上時,Tomcat會為該Web應(yīng)用程序創(chuàng)建一個Context對象,用于管理Web應(yīng)用程序的運行時狀態(tài)。
參數(shù) | 參數(shù)說明 |
Context | 表示一個web應(yīng)用程序,通過為war文件。 |
docBase | 表示W(wǎng)eb應(yīng)用程序的根目錄,即Web應(yīng)用程序的發(fā)布目錄。應(yīng)用程序的路徑或者是WAR文件存放的路徑,也可以使用相對路徑,起始路徑為此Context所屬Host中appBase定義的路徑。 |
path | 表示W(wǎng)eb應(yīng)用程序的上下文路徑,即訪問該Web應(yīng)用程序的URL路徑。 |
reloadable | 這個屬性非常重要,如果為true,則tomcat會白動檢測應(yīng)用程序的/WEB-INF/lib和/WEB-INF/classes目錄的變化,自動裝載新的應(yīng)用程序,可以在不重啟tomcat的情況下改變應(yīng)用程序。 |
crossContext | 用于指定不同的Web應(yīng)用程序之間是否可以共享ServletContext對象。如果crossContext被設(shè)置為true,則表示允許跨上下文共享ServletContext對象,否則不允許。 |
到此這篇關(guān)于Tomcat主配置文件server.xml詳解的文章就介紹到這了,更多相關(guān)Tomcat server.xml內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
tomcat訪問(access)日志配置和記錄Post請求參數(shù)
這篇文章主要介紹了tomcat訪問(access)日志配置和記錄Post請求參數(shù),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03Tomcat添加JMS遠(yuǎn)程監(jiān)控的代碼示例
為Tomcat添加JMS(Java Message Service)遠(yuǎn)程監(jiān)控可以讓你通過消息隊列來接收Tomcat服務(wù)器的性能指標(biāo)和事件通知,下面是一個關(guān)于在Tomcat中添加JMS遠(yuǎn)程監(jiān)控的思維導(dǎo)圖大綱,并給出一些代碼示例和建議,需要的朋友可以參考下2025-02-02使用IDEA配置tomcat及創(chuàng)建JSP文件的方法
這篇文章主要介紹了使用IDEA配置tomcat及創(chuàng)建JSP文件的方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05