How to configure Spring Boot Tomcat

1 Overview

Embedded Web server Spring Boot Web application includes a preconfigured default. However, in some cases, we have to modify the default configuration to meet custom requirements.

In this tutorial, we'll cover application.properties configure some of the common use cases embedded Tomcat server files.

2. The common embedded Tomcat configuration

2.1. Server address and port

We want to change the configuration of the most common is the port number :

server.port = 80
复制代码

If we do not provide server.port parameters, the default setting is 8080 .

In some cases, we may want to set the network address of the server should be bound. In other words, we define an IP address of the server will listen :

server.address = my_custom_ip
复制代码

By default, this value is set to 0.0.0.0 , allowed to connect via all IPv4 addresses. Setting another value, e.g. localhost - 127.0.0.1 - the server will be more selective.

2.2. Error Handling

By default, Spring Boot provides a standard error page . This page is called WhiteLabel . It is enabled by default, but if we do not want to show any error messages, we can disable it:

server.error.whitelabel.enabled = false
复制代码
  • WhitelabelThe default path is * / error *. By setting server.error.path customize its parameters:
server.error.path = /user-error
复制代码

You can also set properties, display information about the error to determine. For example, we can include an error message and stack trace:

server.error.include-exception= true
server.error.include-stacktrace= always
复制代码

Our tutorial Exception Message Handling for REST and [Customize WhiteLabel Error Page] ( www.baeldung.com/ the Spring-the Boot-Custom-error-Page) Detailed Explanation More information about Spring Boot error handling.

2.3. Server Connection

When running on a low-container resources, we may want to reduce the CPU and memory load . One method is to limit the number of requests the application can process simultaneously. Instead, we can increase this value to more use of available resources for better performance.

In the Spring Bootmiddle, we can define the Tomcatmaximum number of worker threads:

server.tomcat.max-threads= 200
复制代码

When you configure a Web server, set the server connection timeout might be useful. This means that the server waits for the client requesting the maximum time before the connection is closed:

server.connection-timeout= 5s
复制代码

We can also define the maximum size of the request header:

server.max-http-header-size= 8KB
复制代码

The maximum size of the request body:

server.tomcat.max-swallow-size= 2MB
复制代码

The maximum size of the entire or POST request:

server.tomcat.max-http-post-size= 2MB
复制代码

2.4. SSL

To enable SSL support in our Spring Boot application , we need to server.ssl.enabled property is set to true , and define the SSL protocol:

server.ssl.enabled = true
server.ssl.protocol = TLS
复制代码

We want to save the configuration certificate key database password, and type the path:

server.ssl.key-store-password=my_password
server.ssl.key-store-type=keystore_type
server.ssl.key-store=keystore-path
复制代码

We must also define alias identifies key repository key:

server.ssl.key-alias=tomcat
复制代码

For more information about SSL configuration, please visit: HTTPS a using Self-Signed Certificate in the Spring the Boot .

2.5. Tomcat server access log

When you try to count the number of page hits, such as user session activity, Tomcat access log is useful.

To enable access log , simply set:

server.tomcat.accesslog.enabled = true
复制代码

We should also configure other parameters, such as attached to the log file directory names, prefixes, suffixes and date formats:

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
复制代码

3. Conclusion

In this tutorial, we learned some common Tomcat embedded server configuration. To see more possible configurations, please visit the official page: the Spring the Boot the Application the Properties docs .

As always, these examples in source code GitHub found on.

Original link: www.baeldung.com/spring-boot...

Author: baeldung

Translator: Leesen

Guess you like

Origin blog.csdn.net/weixin_34191845/article/details/91399647