如果后端返回给前端数据比较大的时候需要考虑开启数据压缩.针对nginx 和tomcat 或spring-boot都可以开启
- nginx配置文件修改
gzip on;
gzip_min_length 1k;
gzip_comp_level 9;
gzip_types application/json text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";
- 针对代理修改
proxy_buffering off;
- spring-boot启用压缩(版本需要大于1.3)
server.compression.enabled=true
server.compression.min-response-size=10
server.compression.excluded-user-agents=gozilla,traviata
server.compression.mime-types=application/json,application/xml,text/html,text/xml,text/plain,application/javascript,text/css
- tomcat启用压缩
<Connector port="8888" protocol="HTTP/1.1"
connectionTimeout="21000"
redirectPort="28080"
URIEncoding="UTF-8"
compression="on"
compressionMinSize="50"
noCompressionUserAgents="gozilla, traviata"
compressableMimeType="text/html,text/xml,text/plain,text/javascript,text/csv,application/javascript,application/json,application/xml" />
以上如果服务器前面有nginx代理,可以使用nginx的提供的数据压缩方式
- 注意
noCompressionUserAgents 针对某些浏览器不启动数据压缩