SpringBoot2 定制和修改嵌入式Servlet容器(Tomcat)的相关配置

SpringBoot默认使用Tomcat作为嵌入式的Servlet容器;

这里写图片描述

1.使用配置文件定制修改相关配置

在application.properties / application.yml配置所需要的属性

属性 描述
server.tomcat.accept-count = 0 #当所有可能的请求处理线程都在使用时,传入连接请求的最大队列长度。
server.tomcat.accesslog.buffered = true #是否缓冲输出以使其仅定期刷新。
server.tomcat.accesslog.directory = logs #创建日志文件的目录。可以是绝对的或相对于Tomcat基础目录。
server.tomcat.accesslog.enabled = false #启用访问日志。
server.tomcat.accesslog.file-date-format = .yyyy-MM-dd #要放在日志文件名中的日期格式。
server.tomcat.accesslog.pattern = common #访问日志的格式模式。
server.tomcat.accesslog.prefix = access_log #日志文件名前缀。
server.tomcat.accesslog.rename-on-rotate = false #是否延迟在文件名中包含日期戳,直到旋转时间。
server.tomcat.accesslog.request-attributes-enabled = false #设置请求的IP地址,主机名,协议和端口的请求属性。
server.tomcat.accesslog.rotate = true #是否启用访问日志轮换。
server.tomcat.accesslog.suffix = .log #日志文件名后缀。
server.tomcat.additional-tld-skip-patterns = #逗号分隔的其他模式列表,这些模式匹配要忽略的TLD扫描的jar。
server.tomcat.background-processor-delay = 30s #调用backgroundProcess方法之间的延迟。如果未指定持续时间后缀,则将使用秒。
server.tomcat.basedir = #Tomcat基目录. Tomcat基目录.如果未指定,则使用临时目录。
server.tomcat.max-http-header-size = 0 #HTTP消息头的最大大小(以字节为单位)。
server.tomcat.max-http-post-size = 0 HTTP帖子内容的最大大小(以字节为单位)。
server.tomcat.max-threads = 0 #最大工作线程数。
server.tomcat.min-spare-threads = 0 #最小工作线程数。
server.tomcat.port-header = X-Forwarded-Port #用于覆盖原始端口值的HTTP标头的名称。
server.tomcat.protocol-header = #包含传入协议的标头,通常命名为“X-Forwarded-Proto”。 #包含传入协议的标头,通常命名为“X-Forwarded-Proto”。
server.tomcat.protocol-header-https-value = https #协议标头的值,指示传入请求是否使用SSL。
server.tomcat.redirect-context-root =#是否应通过在路径中附加/来重定向对上下文根的请求。 #是否应通过在路径中附加/来重定向对上下文根的请求。
server.tomcat.remote-ip-header = #从中提取远程IP的HTTP头的名称。 #从中提取远程IP的HTTP头的名称。例如,X-FORWARDED-FOR
server.tomcat.resource.cache-ttl = #静态资源缓存的生存时间。 #静态资源缓存的生存时间。
server.tomcat.uri-encoding = UTF-8 #用于解码URI的字符编码。
server.tomcat.use-relative-redirects = #通过调用sendRedirect生成的HTTP 1.1和更高版本的位置标头是使用相对还是绝对重定向。 #通过调用sendRedirect生成的HTTP 1.1和更高版本的位置标头是使用相对还是绝对重定向。
server.tomcat.internal-proxies=10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|\\
        192\\.168\\.\\d{1,3}\\.\\d{1,3}|\\
        169\\.254\\.\\d{1,3}\\.\\d{1,3}|\\
        127\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|\\
        172\\.1[6-9]{1}\\.\\d{1,3}\\.\\d{1,3}|\\
        172\\.2[0-9]{1}\\.\\d{1,3}\\.\\d{1,3}|\\
        172\\.3[0-1]{1}\\.\\d{1,3}\\.\\d{1,3} # 正则表达式匹配可信IP地址

官网原版:

server.tomcat.accept-count=0 # Maximum queue length for incoming connection requests when all possible request processing threads are in use.

server.tomcat.accesslog.buffered=true # Whether to buffer output such that it is flushed only periodically.

server.tomcat.accesslog.directory=logs # Directory in which log files are created. Can be absolute or relative to the Tomcat base dir.

server.tomcat.accesslog.enabled=false # Enable access log.

server.tomcat.accesslog.file-date-format=.yyyy-MM-dd # Date format to place in the log file name.

server.tomcat.accesslog.pattern=common # Format pattern for access logs.

server.tomcat.accesslog.prefix=access_log # Log file name prefix.

server.tomcat.accesslog.rename-on-rotate=false # Whether to defer inclusion of the date stamp in the file name until rotate time.

server.tomcat.accesslog.request-attributes-enabled=false # Set request attributes for the IP address, Hostname, protocol, and port used for the request.

server.tomcat.accesslog.rotate=true # Whether to enable access log rotation.

server.tomcat.accesslog.suffix=.log # Log file name suffix.

server.tomcat.additional-tld-skip-patterns= # Comma-separated list of additional patterns that match jars to ignore for TLD scanning.

server.tomcat.background-processor-delay=30s # Delay between the invocation of backgroundProcess methods. If a duration suffix is not specified, seconds will be used.

server.tomcat.basedir= # Tomcat base directory. If not specified, a temporary directory is used.

server.tomcat.internal-proxies=10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|\\
        192\\.168\\.\\d{1,3}\\.\\d{1,3}|\\
        169\\.254\\.\\d{1,3}\\.\\d{1,3}|\\
        127\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|\\
        172\\.1[6-9]{1}\\.\\d{1,3}\\.\\d{1,3}|\\
        172\\.2[0-9]{1}\\.\\d{1,3}\\.\\d{1,3}|\\
        172\\.3[0-1]{1}\\.\\d{1,3}\\.\\d{1,3} # Regular expression matching trusted IP addresses.


server.tomcat.max-http-header-size=0 # Maximum size, in bytes, of the HTTP message header.

server.tomcat.max-http-post-size=0 # Maximum size, in bytes, of the HTTP post content.

server.tomcat.max-threads=0 # Maximum number of worker threads.

server.tomcat.min-spare-threads=0 # Minimum number of worker threads.

server.tomcat.port-header=X-Forwarded-Port # Name of the HTTP header used to override the original port value.

server.tomcat.protocol-header= # Header that holds the incoming protocol, usually named "X-Forwarded-Proto".

server.tomcat.protocol-header-https-value=https # Value of the protocol header indicating whether the incoming request uses SSL.

server.tomcat.redirect-context-root= # Whether requests to the context root should be redirected by appending a / to the path.

server.tomcat.remote-ip-header= # Name of the HTTP header from which the remote IP is extracted. For instance, `X-FORWARDED-FOR`.

server.tomcat.resource.cache-ttl= # Time-to-live of the static resource cache.

server.tomcat.uri-encoding=UTF-8 # Character encoding to use to decode the URI.

2.SpringBoot2.x定制和修改Servlet容器的相关配置,使用配置类

步骤:

1.建立一个配置类,加上@Configuration注解

2.添加定制器ConfigurableServletWebServerFactory

3.将定制器返回

@Configuration
public class TomcatCustomizer {

    @Bean
    public ConfigurableServletWebServerFactory configurableServletWebServerFactory(){
        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
        factory.setPort(8585);
        return factory;
    }
}

这里写图片描述

3.SpringBoot1.x定制和修改Servlet容器的相关配置

@Bean  //一定要将这个定制器加入到容器中
public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer(){
    return new EmbeddedServletContainerCustomizer() {
        //定制嵌入式的Servlet容器相关的规则
        @Override
        public void customize(ConfigurableEmbeddedServletContainer container) {
            container.setPort(8083);
        }
    };
}

猜你喜欢

转载自blog.csdn.net/l1336037686/article/details/81047312