Spring boot embedded tomcat configuration

Spring boot applications start in embedded tomcat container which comes with some pre-configured default behavior via properties file. In this post, we will learn to modify the default tomcat configurations via override respective properties in application.properties file.

1. Overriding embedded tomcat default properties

Below the the default properties options which we can override to customize the behavior of tomcat.

1.1. Server port and address

server.address=my_custom_ip
server.port=80
  • server.address – Server HTTP port. 8080 is default value.
  • server.port – Network address to which the server should bind. Default value is 0.0.0.0 which allows connection via all IPv4 addresses.

1.2. Server connection properties

server.connection-timeout=10s
server.max-http-header-size=8KB

server.tomcat.accept-count=100
server.tomcat.max-connections=10000
server.tomcat.max-threads=200
server.tomcat.min-spare-threads=10
server.tomcat.max-swallow-size=2MB
server.tomcat.max-http-post-size=2MB
  • server.connection-timeout – Time that connectors wait for another HTTP request before closing the connection. When not set, the connector’s container-specific default is used. Use a value of -1 to indicate infinite timeout.
  • server.max-http-header-size – Maximum size of the HTTP message header.
  • server.tomcat.accept-count – Maximum queue length for incoming connection requests when all possible request processing threads are in use.
  • server.tomcat.max-connections – Maximum number of connections that the server accepts and processes at any given time.
  • server.tomcat.max-threads – Maximum amount of worker threads in server under top load. In other words, maximum number of simultaneous requests that can be handled.
  • server.tomcat.min-spare-threads – The minimum number of threads always kept running. This includes both active and idle threads.
  • server.tomcat.max-swallow-size – The maximum number of request body bytes (excluding transfer encoding overhead) that will be swallowed by Tomcat for an aborted upload. An aborted upload is when Tomcat knows that the request body is going to be ignored but the client still sends it. If Tomcat does not swallow the body the client is unlikely to see the response. If not specified the default of 2097152 (2 megabytes) will be used. A value of less than zero indicates that no limit should be enforced.
  • server.tomcat.max-http-post-size – Maximum size of the HTTP post content.

1.3. Error handling

server.error.include-exception=false
server.error.include-stacktrace=never
server.error.path=/error
server.error.whitelabel.enabled=true
  • server.error.include-exception – Include the “exception” attribute while rendering the error view. Default value is false.
  • server.error.include-stacktrace – Include the “stacktrace” attribute while rendering the error view. Default value is never.
  • server.error.path – Default whitelabel error page path url.
  • server.error.whitelabel.enabled – Whether to enable the default error page displayed in browsers in case of a server error.

1.4. Enable/disable HTTPS

server.ssl.enabled=true
server.ssl.protocol=TLS1.2

server.ssl.key-alias=tomcat
server.ssl.key-store=keystore-path
server.ssl.key-store-type=keystore-type
server.ssl.key-store-provider=provider
server.ssl.key-store-password=some-password

server.ssl.trust-store=store-path
server.ssl.trust-store-type=JKS
server.ssl.trust-store-provider=provider
server.ssl.trust-store-password=some-password
  • server.ssl.enabled – Use this attribute to enable SSL traffic on a connector. To turn on SSL handshake/encryption/decryption on a connector set this value to true.
  • server.ssl.protocol – SSL protocol to use. the default is TLS. Pick a value from this list.
  • server.ssl.key-alias – The alias used for the server key and certificate in the keystore. If not specified, the first key read from the keystore will be used.
  • server.ssl.key-store – The pathname of the keystore file where you have stored the server certificate to be loaded.
  • server.ssl.key-store-type – The type of keystore file to be used for the server certificate. If not specified, the default value is “JKS”.
  • server.ssl.key-store-provider – The name of the keystore provider to be used for the server certificate. If not specified, the list of registered providers is traversed in preference order and the first provider that supports the keystoreType is used.
  • server.ssl.key-store-password – The password used to access the specified keystore file. The default value is the value of the keyPass attribute.
  • server.ssl.trust-store – The trust store file to use to validate client certificates.
  • server.ssl.trust-store-type – The type of key store used for the trust store. If that property is not specified, the value of keystore type is used as the default.
  • server.ssl.trust-store-provider – The name of the trust-store provider to be used for the server certificate. If that property is null, the value of keystore provider is used as the default.
  • server.ssl.trust-store-password – The password to access the trust store. If that property is empty, no trust store password will be configured.

1.5. Access Logging

server.tomcat.accesslog.enabled=true
server.tomcat.accesslog.directory=logs
server.tomcat.accesslog.file-date-format=yyyy-MM-dd
server.tomcat.accesslog.prefix=access_log
server.tomcat.accesslog.suffix=.log
server.tomcat.accesslog.rotate=true
  • server.tomcat.accesslog.enabled – Enable access logging or not.
  • server.tomcat.accesslog.directory – Directory in which log files are created. Can be absolute or relative to the Tomcat base dir.
  • server.tomcat.accesslog.file-date-format – Date format to place in the log file name.
  • server.tomcat.accesslog.prefix – Log file name prefix.
  • server.tomcat.accesslog.suffix – Log file name suffix.
  • server.tomcat.accesslog.rotate – Whether to enable access log rotation.

Drop me your questions in comments related to any tomcat configuration discussed above.

Happy Learning !!

Reference :

Spring boot docs

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

5 thoughts on “Spring boot embedded tomcat configuration”

  1. how can we setup our context path for our web app ? i want to setup a folder outside the tomcat directory (file server) where i can store users files .

  2. hello,
    thanks for this blog, but i have a question about the default files that come with tomcat like host manager and examples, how can i disable them in the embeded tomcat ?

Comments are closed.

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.