Spring Boot 启用Gzip压缩

两点需要注意

1、需要在application.properties里启用压缩,并设置压缩支持的格式(默认支持text/html等,但不支持application/json)

server.compression.enabled=true
server.compression.mime-types=application/json,application/xml,text/html,text/xml,text/plain

2、默认情况下,仅会压缩2048字节以上的内容

#默认就是2048 byte
server.compression.min-response-size=2048

 

以上内容,详见 org.springframework.boot.context.embedded.Compression

以及 org.springframework.boot.autoconfigure.web.ServerProperties

 

另外,这个设置本身是由servlet container提供的,所以,也可以在tomcat等的设置里找到。

对tomcat来说,在server.xml 的HTTP/1.1的connector节点中设置:

<!-- 这里的压缩设置,是默认的 -->
<Connector port="8888" protocol="HTTP/1.1" 
               connectionTimeout="21000" 
               redirectPort="28080" 
               URIEncoding="UTF-8"
               compression="on"  
               compressionMinSize="50"
               noCompressionUserAgents="gozilla, traviata"  
               compressionMinSize = “2048”
               compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain" />

 

ps:以上是对于最新的Spring Boot来说的,如果是旧版本的(

 

参考:

Spring 官方指导

使用Tomcat的gzip功能(压缩请求)

tomcat配置gzip压缩compressionMinSize选项无效

Understanding Gzip Compression in Spring Boot ,Angular Java Application

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值