This article is reprinted from http://deep.hongxi.org

Tomcat server. XML has some configuration information that we need to know, at least how to do simple debugging.

 

<Connector port="8080" protocol="HTTP / 1.1" connectionTimeout="20000"  redirectPort="8443" />
Copy the code

  

By default, the configuration information looks like this, and in the dev environment, it’s almost ready to use. The following describes other configuration items that can be attached to the Connector label.

 

 

1. port 

As a network server, Tomcat needs to expose a socket port to accept client links, which can be specified by port.

2. protocol

Using the network protocol, says tomcat using what way to accept the request and deal with the client end, “HTTP / 1.1” is the default, equivalent to “org. Apache. Coyote. Http11. Http11Protocol”; The familiar “AJP/1.3”; Refer to other documentation for the differences and performance benefits between HTTP and AJP. “Document”

After Tomcat 6.0, NIO is also provided, which can effectively improve performance, especially in a large number of long connections/data upload + download and other Web applications. The portocal = “org. Apache. Coyote. Http11. Http11NioProtocol”.

Currently, Tomcat supports BIO, NIO, NIO2, and APR. The default IO model is BIO. For Internet applications, we should choose between NIO and NIO2 because it can effectively improve performance (mainly concurrency). NIO2 is AIO and requires JDK 1.7+ and Linux 2.6+ to support.

BIO: JDK 1.5+, Tomcat 5.x+

NIO: JDK 1.6+, Tomcat 6.x+

NIO2: JDK 1.7+, Tomcat 7.x+

For the sake of conservatism, let’s base it on the NIO model.

3. connectionTimeout

After the client establishes a connection with Tomcat, if the client does not receive the request data within the “connectionTimeout” period, the connection will be disconnected. This value should be set for network stability as well as performance considerations. Different from the TCP configuration option “socket_timeout”,connectionTimeout is valid only after the connection is established and before the client sends an HTTP-Request message.

The default value is 60000, which is 60 seconds. For Internet applications, we should set this value reasonably, such as 20000.

4. maxHeaderCount

The maximum number of headers in an HTTP request. The default is 100. “-1” indicates no limit. If the number of headers in a request exceeds this threshold, the request will be rejected.

5. maxParameterCount

The maximum number of query strings that can be passed in an HTTP-GET request. Although various HTTP proxy tools have restrictions on the length of http-GET requests and the number of query strings, you can still use Tomcat to set the appropriate value again At has a higher memory overhead, and maxParameterCount is often not large for security or practical reasons. The default value is 10000. “-1” indicates no limit. If the number of parameters in a request exceeds the threshold, the request will be rejected.

For security and specification, maxHeaderCount and maxParamterCount should generally be reasonable, with recommended values like 100; If the request has too many parameters, it is recommended to send or split the request using a Post body.

6. maxPostSize

Maximum size of body in an HTTP-POST request, expressed in bytes. The default value is 2M. This affects some form submissions (many text fields). This value can be adjusted appropriately. Large file uploads are usually split into small files in the client rather than sent directly.

7. URIEncoding

The character set used to encode the query string in the HTTP-GET request. The default character set is ISO-8859-1.

8. useBodyEncodingForURI

Whether to encode the query string in an HTTP-GET request using the encoding specified in “Content-type”. If it is “true”, the “URIEncoding” configuration item is ignored and the “Content-type” encoding specified in the header is used instead.

9. maxThreads

MaxThreads specifies the maximum number of threads that tomcat can use to receive and process requests from clients. The default value is 200. Normally, in production environments (depending on the physical machine configuration, or virtual machine constraints), there will be fine tuning. Large values do not increase the load capacity of Tomcat. In fact, “200” threads are large enough. My online environment is maxThreads=120.

For NIO mode, the maxThreads parameter should be determined by the number of CPU cores. For optimism, this value is: number of CPU cores * 2. A value that is too large will not improve NIO performance, but will degrade it because thread switching (CS) will take up a lot of CPU time.

10. compression

Whether to enable Gzip compression for HTTP data. The value can be “off” or “on”. This is a debatable parameter; If compression is enabled, it means less network traffic, but consumes some CPU. If your application has a high CPU performance savings and the response data is a string of text, you can benefit from enabling compression (not all browsers properly support gzip compression, especially earlier versions).

11. acceptCount

When all threads in the Tomcat thread pool are busy, new links will be placed in the pending queue. AcceptCount is the capacity of the queue. If the queue is full, all subsequent link requests will be rejected. The default is 100. In high-concurrency/short-link environments, this value can be appropriately increased; In scenarios with many long links, you can set this value to 0.

This parameter will be entered when the ServerSocket is created and is a TCP low-level parameter. If the requests are short connections and the request time is short, we can increase this value appropriately. 12. address

If multiple IP addresses are bound to the physical server, you can use the address command to specify the address that tomcat-server needs to bind. By default, port is associated with all IP addresses. 13. bufferSize

The size of the buffer data when the stream is read. (Non-socket buffer) 14. ConnectionLinger

Socket linger Parameter value. The time before the socket is closed that the socket is blocked, in seconds. If the value is set to -1, linger is disabled. The default is 100 in BIO(Blocking IO) and AJP links, and 25.15. KeepAliveTimeout in NIO

The amount of time a link is held when there is no actual data interaction, in milliseconds. When this property is not specified, connectionTimeout is used as keepAliveTimeout. Usually works in coordination with the “HTTP keepAlive” option. For HTTP requests, to support high throughput, the server cannot keep an unlimited keepAlive connection (this is fundamentally different from TCP communication in design requirements). After keepAliveTimeout, the connection will be closed. If tomcat is designed to be a “long link” service, you can appropriately increase the keepAliveTimeout value. Otherwise, you do not need to set this value.

 

However, we usually have proxy servers like Nginx on top of Tomcat. We usually want the proxy server to control the keepAlive connection mechanism. For example, nginx decides whether the connection needs to be “kept alive” (as opposed to keep_alive). Almost all links are actively closed after use; Because of the link reuse layer, there will be nginx holding with clients instead of Tomcat holding with clients. Too many keepAlive links, although more efficient, are not good for load balancing. 16. maxKeepAliveRequests

MaxThreads * 0.5 is the maximum number of keepAlive requests that Tomcat needs to hold. It is recommended that the value be not larger than maxThreads; otherwise, the desired effect will not be achieved. -1 indicates that the keepAlive mechanism is disabled.

17, maxConnections

The maximum number of connections that Tomcat allows to be received and processed defaults to the same value as the maxThreads parameter for BIO and to 10000 for NIO. If the number of threads tomcat has accepted and is processing reaches this value, the server will allow it to continue accepting new links but will not process them, and these links will be blocked until the number of connections drops below this value (instead of reading data from these sockets, the server will buffer their handles). Ultimately, whether the server can continue accepting new links depends on the acceptCount value, because this value is the parameter passed when the ServerSocket is created, after which the link request will be rejected.

This value is also limited by the system’s Ulimit, CPU, memory, and other configurations.

AcceptorThreadCount: The default value is 1, indicating the number of threads used to accept new links. This value can be set to 2 on multi-core CPUS. It is not recommended to set more than 2.

MaxHttpHeaderSize: specifies the maximum size of the HTTP header. The default value is 8192, expressed in bytes.

MinSpareThreads: Minimum number of active threads in the thread pool. Default: 10.

SSLEnabled: Specifies whether to enable SSL. The default value is false. Generally SSL should be in the proxy layer such as Nginx, we should not let Tomcat access directly.

 

[NIO configuration is as follows]

PollerThreadCount: Indicates the number of threads used to polling IO events. The default is 1. On a multi-core CPU architecture, we can set it to 2 to improve polling capability. A value greater than 2 is not officially recommended because lock contention can degrade performance. In fact, a single thread is fast enough.

23. UseSendfile: specifies whether to enable sendFile. The default value is true. For Web applications, usually a project contains a certain number of static resources, such as images, CSS, JS, HTML, etc. Sendfile can improve performance to a certain extent.

SelectorTimeout: the length of time a selector blocks. If you have done NIO development, you should know what this parameter means. The default value is 1000 milliseconds. This value is not too large, because the Selector thread itself also needs to clean up closed links and so on.

25, selectorPool. MaxSelectors: select the number of NIO, the default value is 200. In NIO, we can use multiple selectors, and each selector is responsible for registering a certain number of Niochannels, which can effectively improve the efficiency of selector selection. It is usually recommended that this value be the same as maxThreads or less than maxThreads, but greater than maxThreads doesn’t really mean much.

To enable this feature, we need to add a startup command parameter to catalina.sh:

CATALINA_OPTS="-Dorg.apache.tomcat.util.net.NioSelectorShared=false"
Copy the code

Official meaning is to use the command – line – options = “- Dorg.apache.tomcat.util.net.NioSelectorShared=false”, but after tests found does not take effect. This command argument means “instead of using a shared selector, each thread uses its own selector.”

<Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol"  
               connectionTimeout="20000"  
               maxHeaderCount="64"  
               maxParameterCount="64"  
               maxHttpHeaderSize="8192"  
               URIEncoding="UTF-8"  
               useBodyEncodingForURI="false"  
               maxThreads="128"  
               minSpareThreads="12"  
               acceptCount="1024"  
               connectionLinger="1"  
               keepAliveTimeout="60"  
               maxKeepAliveRequests="32"  
               maxConnections="10000"  
               acceptorThreadCount="1"  
               pollerThreadCount="2"  
               selectorTimeout="1000"  
               useSendfile="true"  
               selectorPool.maxSelectors="128"  
               redirectPort="8443" />
Copy the code

  

 

Reference:

Tomcat.apache.org/tomcat-7.0-…

Tomcat.apache.org/tomcat-8.0-…