The configuration of the Tomcat
server mainly focuses on
the catalina.policy , catalina.properties, context.xml , server.xml , tomcat-users.xml , and web.xml files under
tomcat/conf .
One: server.xml
server.xml
is the core configuration file of the
tomcat
server, including all the configurations of
Tomcat
's
Servlet
container (Catalina
). Since there are so many configuration attributes, we mainly explain some of the important configurations here.
1.Server
Server
is the root element of
server.xml
, used to create a
Server
instance, the default implementation class is org.apache.catalina.core.StandardServer.
<Server port="8005" shutdown="SHUTDOWN">
...
</Server>
port :
The port of the shutdown server that Tomcat listens on.
shutdown
: The command string to shut down the server.
The sub-elements embedded in
Server are
Listener
,
GlobalNamingResources
, and
Service
.
The meaning of the
five
Listeners configured by default :
<!‐‐ 用于以日志形式输出服务器 、操作系统、JVM的版本信息 ‐‐>
<Listener className="org.apache.catalina.startup.VersionLoggerListener"
/>
<!‐‐ 用于加载(服务器启动) 和 销毁 (服务器停止) APR。 如果找不到APR库, 则会
输出日志, 并不影响Tomcat启动 ‐‐>
<Listener className="org.apache.catalina.core.AprLifecycleListener"
SSLEngine="on" />
<!‐‐ 用于避免JRE内存泄漏问题 ‐‐>
<Listener
className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<!‐‐ 用户加载(服务器启动) 和 销毁(服务器停止) 全局命名服务 ‐‐>
<Listener
className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"
/>
<!‐‐ 用于在Context停止时重建Executor 池中的线程, 以避免ThreadLocal 相关的内
存泄漏 ‐‐>
<Listener
className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
The global naming service is defined in GlobalNamingResources :
<!‐‐ 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>
2.Service
This element is used to create a
Service
instance,
org.apache.catalina.core.StandardService is used by default
. By default, Tomcat
only specifies the name of
the Service
, and the value is
"Catalina"
.
The elements that can be embedded in
Service are: Listener
, Executor , Connector , Engine , among which: Listener is used to add a lifecycle listener to Service, Executor is used to configure the shared thread pool of Service , Connector is used to configure the linker contained in Service, and Engine is used to To configure the Servlet container engine corresponding to the linker in the Service .
<Service name="Catalina">
...
</Service>
A
Server
server can contain multiple
Service
services.
3 .I execute
By default,
Service
does not add shared thread pool configuration. If we want to add a thread pool, we can add the following configuration below:
<Executor name="tomcatThreadPool"
namePrefix="catalina‐exec‐"
maxThreads="200"
minSpareThreads="100"
maxIdleTime="60000"
maxQueueSize="Integer.MAX_VALUE"
prestartminSpareThreads="false"
threadPriority="5"
className="org.apache.catalina.core.StandardThreadExecutor"/>
Property description:
If the shared thread pool is not configured, each Catalina component will be created independently when using the thread pool.
4 .Connector
Connector
is used to create linker instances. By default,
server.xml
configures two linkers, one supports HTTP
protocol and the other supports
AJP
protocol. Therefore, in most cases, we do not need to add a new linker configuration, but just optimize the existing linker as needed.
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
Property description:
1
)
port
: Port number,
Connector
is used to create a server
Socket
and monitor it to wait for the client to request a connection. If this property is set to 0
,
Tomcat
will randomly select an available port number for the current
Connector to use.
2
)
protocol
:
the access protocol supported by the current
Connector .
The default is
HTTP/1.1
, and an automatic switching mechanism is used to select a
linker based on JAVA NIO or
a linker based on local
APR (judging according to whether the local library contains Tomcat).
If you do not want to use the above automatic switching mechanism, but specify the protocol explicitly, the following values can be used.
Http
protocol:
org.apache.coyote.http11.Http11NioProtocol , 非阻塞式 Java NIO 链接器
org.apache.coyote.http11.Http11Nio2Protocol , 非阻塞式 JAVA NIO2 链接器
org.apache.coyote.http11.Http11AprProtocol , APR 链接器
AJP
protocol:
org.apache.coyote.ajp.AjpNioProtocol , non-blocking Java NIO linkerorg.apache.coyote.ajp.AjpNio2Protocol , non-blocking JAVA NIO2 linkerorg.apache.coyote.ajp.AjpAprProtocol , the APR linker
3
)
connectionTimeOut:
The waiting timeout time after the Connector receives the link, in milliseconds.
-1
means no timeout.
4
)
redirectPort
: The current
Connector
does not support
SSL
requests. A request is received, and it also complies with the security-constraint constraints.
SSL
transmission is required, and
Catalina
automatically redirects the request to the specified port.
5
)
executor
: Specify the name of the shared thread pool, or
configure the internal thread pool through attributes such as
maxThreads
and minSpareThreads.
6
)
URIEncoding:
It is used to specify
the character encoding of the encoded
URI .
The default encoding of the Tomcat8.x version is UTF-8, and the default encoding of the Tomcat7.x version is ISO-8859-1 .
The complete configuration is as follows:
<Connector port="8080"
protocol="HTTP/1.1"
executor="tomcatThreadPool"
maxThreads="1000"
minSpareThreads="100"
acceptCount="1000"
maxConnections="1000"
connectionTimeout="20000"
compression="on"
compressionMinSize="2048"
disableUploadTimeout="true"
redirectPort="8443"
URIEncoding="UTF‐8" />
5.Engine
Engine
, as the top-level element of the
Servlet
engine, can be embedded inside:
Cluster
,
Listener
,
Realm
, Valve and
Host
.
<Engine name="Catalina" defaultHost="localhost">
...
</Engine>
Property description:
1
)
name
: used to specify the name of
the Engine
, the default is
Catalina
. The name affects part of
Tomcat
's
Storage path (such as temporary files).
2
)
defaultHost
: The name of the virtual host used by default, when the host requested by the client is invalid, it will be sent
Handled by the default virtual host, which defaults to
localhost
.
6 .Host
The Host
element is used to configure a virtual host, which supports the following embedded elements:
Alias
,
Cluster
,
Listener
, Valve,
Realm
,
Context
. If
Realm is configured under
Engine
, then this configuration will be shared among all Hosts under the current Engine . Similarly, if Realm is configured in Host , it will be shared among all Contexts under the current Host . Realm priority in Context > Realm priority in Host > Realm priority in Engine.
<Host name="localhost" appBase="webapps" unpackWARs="true"
autoDeploy="true">
...
</Host>
Property description:
1
)
name:
The common network name of the
current
Host , which must
be consistent with the registration information on the DNS server. Engine _
The included
Host
must have a name that is consistent with
the Engine
's
defaultHost
setting.
2
)
appBase
:
the application base directory of the current
Host , and all the
web applications deployed on the current Host are in this directory
(It can be an absolute directory, a relative path). Defaults to
webapps
.
3
)
unpackWARs
: set to
true
,
the host
will
decompress the war package under
the appBase directory as the target
record. Set to
false
,
the Host
will start directly from
the war
file.
4
)
autoDeploy
: Control whether
tomcat periodically detects and automatically deploys new or changed
web applications
during runtime
use.
By adding an alias to
the Host
, we can realize that the same
Host
has multiple network names, the configuration is as follows:
<Host name="www.web1.com" appBase="webapps" unpackWARs="true"
autoDeploy="true">
<Alias>www.web2.com</Alias>
</Host>
At this time, we can access
the application under the current
Host through the two domain names (need to ensure that
DNS or hosts are added
Added domain name mapping configuration).
7.Context
Context
is used to configure a
web
application, the default configuration is as follows:
<Context docBase="myApp" path="/myApp">
....
</Context>
Property description:
1
)
docBase
:
Web
application directory or deployment path of
War
package. Can be an absolute path or relative to
Relative path to Host appBase .
2
)
path
: the
Context path of
the web application
. If our Host name is localhost , then the web application access
The root path is:
http://localhost:8080/myApp
.
The embedded elements it supports are:
CookieProcessor
,
Loader
,
Manager
,
Realm
,
Resources
,
WatchedResource
,
JarScanner
,
Valve
。
<Host name="www.tomcat.com" appBase="webapps" unpackWARs="true"
autoDeploy="true">
<Context docBase="D:\servlet_project03" path="/myApp"></Context>
<Valve className="org.apache.catalina.valves.AccessLogValve"
directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
二:tomcat-users.xml
In this configuration file, the Tomcat
user, role and other information
are mainly configured , which are used to control the access rights of the manager and host-manager in
Tomcat .