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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM