Learn How to Configure the Embedded Tomcat Server in a Spring Boot Application.
All Spring Boot applications, by default, run on the embedded tomcat servers. When we run a Spring Boot application, it internally launches the embedded tomcat server. The Embedded tomcat server has a set of default configurations, which makes them ready to use. However, we can use the properties or YAML files to configure the embedded tomcat server and change the default settings.
We will start with Tomcat’s basic configurations like server address and port. Then, we will move to enable and change the server’s access logs, error handling and server connection settings. Finally, we will configure the SSL Settings of the embedded server.
Modify Tomcat Server and Port
The following example shows how to configure the server address and port number. The Tomcat, by default, listens on port 8080 and the network address of 0.0.0.0.
server.address=ip_address Code language: Properties (properties)
In most cases, we will be changing the default port number of an application, and there are several ways of doing that in Spring Boot. Please read: How to Change the Default Port in the Spring Boot Application.
Enable and Configure Tomcat Access Logs
The Spring Boot, by default, disables access log settings in the embedded tomcat server. However, we can enable them using the ‘server.tomcat.accesslog.enabled‘ property.
server.tomcat.accesslog.enabled=trueCode language: Properties (properties)
The following snippets show how to configure the access logs settings in Tomcat.
Modify Access Log Directory or Location in Tomcat
server.tomcat.accesslog.directory=logsCode language: Properties (properties)
Or, change the suffix or prefix of the access log file’s name in Tomcat
server.tomcat.accesslog.suffix=.logCode language: Properties (properties)
Similarly, we can modify the date format in the access log file names
server.tomcat.accesslog.file-date-format=yyyyMMddCode language: Properties (properties)
By default, Tomcat keeps the access logs forever. However, we can configure TTL settings for Tomcat’s access logs that specify the number of days it retains the access logs.
server.tomcat.accesslog.max-days=14Code language: Properties (properties)
Configure Tomcat Error Handling
Upon server errors, Tomcat displays a generic error page, the Whitelabel page, in the browser. However, Spring Boot’s embedded Tomcat allows us to disable the Whitelabel error page.
server.error.whitelabel.enabled=falseCode language: Properties (properties)
On top of this, Spring Boot also allows us to fine-tune the embedded Tomcat’s error details. Using that, we can add the exception details, stack trace and error message on the error page.
server.error.include-message=alwaysCode language: Properties (properties)
Configure Tomcat Server Connections
Tomcat server has an inbuilt thread pooling mechanism that allows the server to handle several requests concurrently. However, for the effective and optimal use of the available resources like memory and CPU, we can set the maximum number of concurrent threads the server creates. If not specified, the value defaults to 200 threads.
server.tomcat.threads.max=100Code language: Properties (properties)
Moreover, we can also specify how the Minimum number of Threads should always remain in the pool, which isten0 by default.
server.tomcat.threads.min-spare=5Code language: Properties (properties)
Once a client connects with the server, the server waits for the request to be complete. We can specify the Tomcat Server Connection Timeout value. After the timeout value is exceeded, the server will close the client connection.
server.connection-timeout=10sCode language: Properties (properties)
We can also control the size of the requests reaching the server. Let’s see them one by one.
We can Set Maximum HTTP Header Size
server.max-http-header-size=8KBCode language: Properties (properties)
And we can set the Maximum HTTP Request Size.
server.tomcat.max-swallow-size=2MBCode language: Properties (properties)
Also, we can set the Maximum Post Request size.
server.tomcat.max-http-post-size=4MBCode language: Properties (properties)
We will look at How to Configure SSL in a Spring Boot Application.
Enable the Tomcat SSL `
server.ssl.enabled=trueCode language: Properties (properties)
Configure the SSL Protocol
server.ssl.protocol=TLSCode language: Properties (properties)
An alias is used for the key in the key store. We can specify the alias using the ‘server.ssl.key-alias‘ property. If we do not specify it, the first key in the key store is used.
server.ssl.key-alias=tomcatCode language: Properties (properties)
We can set the certificate key store path, key store type, key store provider, and key store password like this.
server.ssl.key-store-password=keystore_passwordCode language: Properties (properties)
The client certificates are validated using a trust store. The trust store properties can be provided like this. These properties include the trust store path, trust store type, provider, and password.
server.ssl.trust-store-password=ts-passwordCode language: Properties (properties)
This quick tutorial covered various ways of Configuring the Embedded Tomcat Server in a Spring Boot Application. We learned to configure the tomcat address and port, enable and configure Tomcat Access logs, Configure and Change the default Error Handling, and Configure the Tomcat Connection properties. Finally, we learned to Configure Tomcat SSL properties.