JavaSpringTechnology

Configure Spring Boot Embedded Tomcat Server

Learn How to Configure Embedded Tomcat Server in a Spring Boot Application.

Overview

Spring Boot ships with an Embedded Tomcat Server. When we run a Spring Boot Application, the embedded tomcat server is started and the application is launched inside the 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 embedded tomcat server and change the default settings.

We will start with the most basic Tomcat configurations like server address and port. Then, we will move to enable and change the servers access logs setting. Next, we will change the error handling and server connections behaviour. Finally, we will configure the SSL Settings of the embedded server.

Modify Address and Port

Next example shows, how to configure server address and port number. The tomcat by default, listens on the port 8080 and the network address of 0.0.0.0.

server.port=8181
server.address=ip_address 

In most of the cases, we may want to change the port number of the Tomcat and there are more than one way of doing that. To learn them visit our article Change Default Port in Spring Boot Application.

Enable and Configure Tomcat Access Logs

The access logs are by default disabled in tomcat. We can enable them using the next configuration.

server.tomcat.accesslog.enabled=true

The next section shows how to configure some of the common properties of the access logs.

To Change the access log directory:

server.tomcat.accesslog.directory=logs

Or, to change the prefix and suffix appended with the access log file names:

server.tomcat.accesslog.prefix=access
server.tomcat.accesslog.suffix=.log

Similarly we can also change the date format which is used in the access log file names:

server.tomcat.accesslog.file-date-format=yyyyMMdd

By default, tomcat keeps the access logs forever. However, we can change this and provide the number of days, we want the logs to be retained.

server.tomcat.accesslog.max-days=14

Configure Tomcat Error Handling

Tomcat has a default mechanism to display a generic error page when a the server throws exception. We can disable this error page by setting this.

server.error.whitelabel.enabled=false

Using the next set of properties, we can control what information about the error is displayed on the error page.

In the next example, we are allowing the Exception to be displayed on the error page, however hiding the display of complete exception stack trace. We are also allowing the error message to be displayed on the page.

server.error.include-exception=true
server.error.include-stacktrace=never
server.error.include-message=always

Configure Tomcat Server Connections

The server handles a number of concurrent requests by using its in-build thread pooling mechanism. 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 set, the value defaults to 200 threads.

server.tomcat.threads.max=100

Moreover, we can also specify how Minimum number of Threads should always remain in the pool, which is 10 by default.

server.tomcat.threads.min-spare=5

Once a client makes a connection 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=10s

We can also control the size of the requests reaching to the server. Let’s see them one by one.

We can Set Maximum Http Header Size

server.max-http-header-size=8KB

And, we can Set Maximum Http Request Size. This is the

server.tomcat.max-swallow-size=2MB

Also, we can Set Maximum Post Request size.

server.tomcat.max-http-post-size

Configure SSL

We will have a look at How to Configure SSL in a Spring Boot Application.

Enable the Tomcat SSL `

server.ssl.enabled=true

Configure the SSL Protocol

server.ssl.protocol=TLS

An alias is used to identify the key in key store. We can specify the alias using the next property. If we do not specify it, the first key in the key store is used.

server.ssl.key-alias=tomcat

We can set the certificate key store path, key store type, key store provider, and key store password like this.

server.ssl.key-store=keystore_path
server.ssl.key-store-type=keystore_type
server.ssl.key-store-provider=keystore_provider
server.ssl.key-store-password=keystore_password

The client certificates are validated using a trust store. The trust store properties can be provided like this. These properties include trust store path, trust store type, trust store provider, and the trust store password.

server.ssl.trust-store=store-ts_path
server.ssl.trust-store-type=JKS
server.ssl.trust-store-provider=ts_provider
server.ssl.trust-store-password=ts-password

Summary

In this quick tutorial we covered various ways of Configuring Embedded Tomcat Server in a Spring Boot Application. We learned to configure tomcat address, and port, enable and configure Tomcat Access logs, Configure and Change the default Error Handling, Configure the Tomcat Connection properties, and finally we learned to Configure Tomcat SSL properties.