from:http://blog.itblacklist.cn/20161230/8442.html
首先nginx支持gzip,增加配置:
gzip on;#用于开启或关闭gzip模块(on/off)
gzip_min_length 1k;#设置允许压缩的页面最小字节数,页面字节数从header头得content-length中进行获取。
#默认值是0,不管页面多大都压缩。建议设置成大于1k的字节数,小于1k可能会越压越大。
gzip_buffers 16 64k;#设置系统获取几个单位的缓存用于存储gzip的压缩结果数据流。
#16 16k代表以16k为单位,安装原始数据大小以16k为单位的16倍申请内存。
gzip_http_version 1.1;#识别http的协议版本(1.0/1.1)
gzip_comp_level 6;#gzip压缩比,1压缩比最小处理速度最快,9压缩比最大但处理速度最慢(传输快但比较消耗cpu)
gzip_types application/javascript text/css image/jpeg;#匹配mime类型进行压缩,无论是否指定,”text/html”类型总是会被压缩
gzip_vary on;#和http头有关系,加个vary头,给代理服务器用的,有的浏览器支持压缩,有的不支持,
#所以避免浪费不支持的也压缩,所以根据客户端的HTTP头来判断,是否需要压缩
重启nginx:
nginx -s reload
修改springboot配置application.properties:
server.compression.enabled=true
server.compression.mime-types=application/json,application/xml,text/html,text/xml,text/plain
至此结束,response支持gzip。
=================================================================================================
附springboot其它版本实现:
#springboot 1.2.2 - 1.3(不包含)
server.tomcat.compression: on
server.tomcat.compressableMimeTypes=application/json,application/xml,text/html,text/xml,text/plain
#低于springboot 1.2.2
@Component
public class TomcatCustomizer implements TomcatConnectorCustomizer {
@Override
public void customize(Connector connector) {
connector.setProperty("compression", "on");
connector.setProperty("compressableMimeType", "text/html,text/xml,text/plain,application/json,application/xml");
}
}