This tutorial guides you through customizing Spring Boot’s embedded Tomcat web server for optimal performance and security. The embedded server eliminates the need for a separate application server like traditional Tomcat installations.
Tomcat on Spring Boot Key properties
By default, you can configure a standalone Tomcat Web Server in the configuration file server.xml. When you run Tomcat inside a Spring Boot application, you will need to translate the Web Server configuration in Spring Boot format. The most common configuration sources are application.properties or application.yml. Let’s see some Key Configuration Properties in Tomcat
Configuring the Tomcat Port
Here is an example application.properties configuration which sets the port 8180 for the Tomcat Web Server
# Server HTTP port server.port=8180
Tomcat Server Port is part of the ServerProperties, which are loaded through Spring Boot’s auto-configuration: org.springframework.boot.autoconfigure.web.ServerProperties
Finally, you can discover the port the server is running on from log output or from the ServletWebServerApplicationContext through its WebServer. Here is an example of it:
public class MyWebIntegrationTests { @Autowired ServletWebServerApplicationContext server; @LocalServerPort int port; }
Configuring Tomcat Connection Properties
Customizing these properties in Tomcat embedded within Spring Boot allows you to fine-tune your server’s performance and resource management to better match your application’s needs and expected traffic patterns.
Here are some sample Connection Properties:
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
And here is an explanation to the above properties:
server.tomcat.accept-count=100
: Defines the number of incoming connection requests Tomcat can hold in a queue before refusing them. Set this based on expected traffic volume. A higher value allows more connections to queue, but requires more resources.
server.tomcat.max-connections=10000
: Sets the maximum number of concurrent connections Tomcat can handle. Adjust this based on your application’s expected user base and workload. A higher value allows more users, but requires more server resources.
server.tomcat.max-threads=200
: Defines the maximum number of worker threads Tomcat can use to process requests. Tune this based on your application’s processing needs. More threads handle concurrent requests better, but come at a resource cost.
server.tomcat.min-spare-threads=10
: Sets the minimum number of idle worker threads Tomcat keeps available to handle incoming requests immediately. Adjust this for faster response times upon sudden traffic spikes.
Configuring Request Handling in Tomcat
The following properties will allow you to fine-tune some crucial request handling properties:
server.tomcat.max-swallow-size=2MB server.tomcat.max-http-post-size=2MB
Here is an explanation to these properties:
server.tomcat.max-swallow-size=2MB
: Defines the maximum size of data Tomcat will read from the client before throwing an error. This protects against malicious requests with excessively large payloads.
server.tomcat.max-http-post-size=2MB
: Sets the maximum size of HTTP POST requests Tomcat can handle. Increase this if your application handles large file uploads.
Configuring Access Logs
Access logs are a key log file to trace all incoming HTTP Requests. You can enable Access Logs in Spring Boot through the following sample configuration:
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
Here is an explanation to the above properties:
server.tomcat.accesslog.enabled=true
: Enables access logging for Tomcat. This records details about each request (IP address, URL, etc.).
server.tomcat.accesslog.directory=logs
: Specifies the directory where access logs will be stored (e.g., “logs”).
server.tomcat.accesslog.file-date-format=yyyy-MM-dd
: Defines the date format for log file names.
server.tomcat.accesslog.prefix=access_log
: Sets the prefix for access log filenames (e.g., “access_log”).
server.tomcat.accesslog.suffix=.log
: Defines the file extension for access logs (e.g., “.log”).
server.tomcat.accesslog.rotate=true
: Enables rotating access logs when they reach a certain size. This helps manage disk space.
How to configure Tomcat properties programmatically
Besides using the configuration files, you can also configure Spring Boot’s embedded Web Server programmatically. The Class to use is org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory
which is a Factory to Tomcat’s Properties.
For example, here is a sample programmatic configuration:
import ; import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.stereotype.Component; @Component public class TomcatCustomizer implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> { @Override public void customize(TomcatServletWebServerFactory factory) { factory.setPort(8080); } }
Conclusion
This article was a walk through the configuration of some common Tomcat Properties in a Spring Boot application.
If you want to switch to Undertow Web Server, check this tutorial: Configure Spring Boot to use Undertow Web server
If you want to switch to Jetty Web Server, check this tutorial: Configure Spring Boot to use Jetty Server
Found the article helpful? if so please follow us on Socials