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.
8181 =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.
true=Code language: Properties (properties)
The following snippets show how to configure the access logs settings in Tomcat.
Modify Access Log Directory or Location in Tomcat
logs=Code language: Properties (properties)
Or, change the suffix or prefix of the access log file’s name in Tomcat
access =.log=Code language: Properties (properties)
Similarly, we can modify the date format in the access log file names
yyyyMMdd=Code 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.
14=Code 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.
false=Code 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.
true =never =always=Code 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.
100=Code language: Properties (properties)
Moreover, we can also specify how the Minimum number of Threads should always remain in the pool, which isten0 by default.
5=Code 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.
10s=Code 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
8KB=Code language: Properties (properties)
And we can set the Maximum HTTP Request Size.
2MB=Code language: Properties (properties)
Also, we can set the Maximum Post Request size.
4MB=Code language: Properties (properties)
We will look at How to Configure SSL in a Spring Boot Application.
Enable the Tomcat SSL `
true=Code language: Properties (properties)
Configure the SSL Protocol
TLS=Code 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.
tomcat=Code language: Properties (properties)
We can set the certificate key store path, key store type, key store provider, and key store password like this.
keystore_path =keystore_type =keystore_provider =keystore_password=Code 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.
store-ts_path =JKS =ts_provider =ts-password=Code 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.