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