8

I am trying to fix/debug an issue of too many closing connection in a spring-boot web app that uses embedded tomcat. The problem arise because it closes connection that should be kept alive.

Now, I found that tomcat has configuration that limit the number of keep-alive connection (see maxKeepAliveRequests in https://tomcat.apache.org/tomcat-8.5-doc/config/http.html) and there are maybe other config that could be related to the issue. But my problem is that I don't see where are those parameters given, or how I could change them if default are used.

My question: where can I found a documentation that explain how to config spring-boot/embedded-tomcat keep-alive parameters, and which are those parameters?

Ken White
  • 123,280
  • 14
  • 225
  • 444
Juh_
  • 14,628
  • 8
  • 59
  • 92

4 Answers4

3

Not all tomcat properties can be configured via the properties file. The keep-alive related properties are one of those properties and that means they can only be configured programmatically. This is done by configuring a WebServerFactoryCustomizer bean. You can use the protocol handler to set the KeepAlive settings. An example below:

@Bean
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> tomcatCustomizer() {
    return (tomcat) -> tomcat.addConnectorCustomizers((connector) -> {
        if (connector.getProtocolHandler() instanceof AbstractHttp11Protocol) {
            AbstractHttp11Protocol<?> protocolHandler = (AbstractHttp11Protocol<?>) connector
                    .getProtocolHandler();
            protocolHandler.setKeepAliveTimeout(80000);
            protocolHandler.setMaxKeepAliveRequests(500);
            protocolHandler.setUseKeepAliveResponseHeader(true);
        }
    });
}

To know more about these settings please read the tomcat 9 configuration reference

Maurice
  • 6,698
  • 9
  • 47
  • 104
  • 2
    I don't have this problem anymore and I can't test it, but this looks to be a good solution. So I'll select it. Thanks – Juh_ Jan 17 '22 at 08:42
0

Found this in the documentation shared by Dina Bogdan, and I was told that those properties vary depending on spring boot release version.

Rick Choi
  • 29
  • 6
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 30 '22 at 15:01
  • Also, your answer should be self contains, links are just to provide additional details or reference link – Juh_ Jul 01 '22 at 12:32
0

The Spring Boot properties for controlling Tomcat keep-alive are:

server.tomcat.max-keep-alive-requests=100

Number of keep-alive sessions, default is 100

server.tomcat.keep-alive-timeout=60000

Number of msec of inactivity to keep each session alive

-2

These are the configuration-properties for tomcat server:

server.tomcat.accept-count=100 # Maximum queue length for incoming connection requests when all possible request processing threads are in use.
server.tomcat.accesslog.buffered=true # Whether to buffer output such that it is flushed only periodically.
server.tomcat.accesslog.directory=logs # Directory in which log files are created. Can be absolute or relative to the Tomcat base dir.
server.tomcat.accesslog.enabled=false # Enable access log.
server.tomcat.accesslog.file-date-format=.yyyy-MM-dd # Date format to place in the log file name.
server.tomcat.accesslog.pattern=common # Format pattern for access logs.
server.tomcat.accesslog.prefix=access_log # Log file name prefix.
server.tomcat.accesslog.rename-on-rotate=false # Whether to defer inclusion of the date stamp in the file name until rotate time.
server.tomcat.accesslog.request-attributes-enabled=false # Set request attributes for the IP address, Hostname, protocol, and port used for the request.
server.tomcat.accesslog.rotate=true # Whether to enable access log rotation.
server.tomcat.accesslog.suffix=.log # Log file name suffix.
server.tomcat.additional-tld-skip-patterns= # Comma-separated list of additional patterns that match jars to ignore for TLD scanning.
server.tomcat.background-processor-delay=10s # Delay between the invocation of backgroundProcess methods. If a duration suffix is not specified, seconds will be used.
server.tomcat.basedir= # Tomcat base directory. If not specified, a temporary directory is used.
server.tomcat.internal-proxies=10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|\\
        192\\.168\\.\\d{1,3}\\.\\d{1,3}|\\
        169\\.254\\.\\d{1,3}\\.\\d{1,3}|\\
        127\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|\\
        172\\.1[6-9]{1}\\.\\d{1,3}\\.\\d{1,3}|\\
        172\\.2[0-9]{1}\\.\\d{1,3}\\.\\d{1,3}|\\
        172\\.3[0-1]{1}\\.\\d{1,3}\\.\\d{1,3}\\
        0:0:0:0:0:0:0:1\\
        ::1 # Regular expression that matches proxies that are to be trusted.
server.tomcat.max-connections=10000 # Maximum number of connections that the server accepts and processes at any given time.
server.tomcat.max-http-post-size=2MB # Maximum size of the HTTP post content.
server.tomcat.max-swallow-size=2MB # Maximum amount of request body to swallow.
server.tomcat.max-threads=200 # Maximum amount of worker threads.
server.tomcat.min-spare-threads=10 # Minimum amount of worker threads.
server.tomcat.port-header=X-Forwarded-Port # Name of the HTTP header used to override the original port value.
server.tomcat.protocol-header= # Header that holds the incoming protocol, usually named "X-Forwarded-Proto".
server.tomcat.protocol-header-https-value=https # Value of the protocol header indicating whether the incoming request uses SSL.
server.tomcat.redirect-context-root=true # Whether requests to the context root should be redirected by appending a / to the path.
server.tomcat.remote-ip-header= # Name of the HTTP header from which the remote IP is extracted. For instance, `X-FORWARDED-FOR`.
server.tomcat.resource.allow-caching=true # Whether static resource caching is permitted for this web application.
server.tomcat.resource.cache-ttl= # Time-to-live of the static resource cache.
server.tomcat.uri-encoding=UTF-8 # Character encoding to use to decode the URI.
server.tomcat.use-relative-redirects= # Whether HTTP 1.1 and later location headers generated by a call to sendRedirect will use relative or absolute redirects.

For more details check here.

Dina Bogdan
  • 4,345
  • 5
  • 27
  • 56