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地址
1
2
3
4
5
6
7
官网原版:

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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;
    }
}
1
2
3
4
5
6
7
8
9
10
11


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

转载于:https://my.oschina.net/xiaominmin/blog/3061710

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中,可以通过配置文件来自定义Tomcat配置。默认情况下,Spring Boot使用嵌入式Tomcat作为Servlet容器Tomcat的默认配置参数可以在ServerProperties类中找到,其中的静态内部类Threads中定义了最大工作线程数(worker threads)的默认值。 关于Tomcat配置,可以在application.properties文件中进行配置。通用的Servlet容器配置以"server"作为前缀,而Tomcat特有的配置以"server.tomcat"作为前缀。 以下是一些常用的Tomcat配置示例: - 设置Tomcat的端口号: `server.port=8080` - 设置Tomcat的上下文路径: `server.servlet.context-path=/myapp` - 配置Tomcat的连接器属性: `server.tomcat.*` 请注意,以上只是一些常见的配置示例,你还可以根据需要进行更多的Tomcat配置。通过在application.properties文件中添加对应的属性,可以根据需求来自定义Tomcat的行为。 你也可以参考Spring Boot官方文档中关于Tomcat配置的部分进行更详细的了解和配置。 - [ServerProperties - Spring Boot Reference Guide](https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html#server-properties) - [Spring Boot Documentation - Embedded Servlet Containers](https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-embedded-container) - [Spring Boot Documentation - Common Application Properties](https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#common-application-properties)<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [[SpringBoot Server]SpringBoot tomcat配置](https://blog.csdn.net/mdwsmg/article/details/126830647)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *3* [SpringBootTomcat配置(学习SpringBoot实战)](https://blog.csdn.net/m0_66557301/article/details/124418970)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值