nginx实现资源压缩的原理是经过ngx_http_gzip_module模块拦截请求,并对须要作gzip的类型作gzip压缩,该模块是默认基础的,不须要从新编译,直接开启便可。
# 开启gzip gzip on; # 启用gzip压缩的最小文件,小于设置值的文件将不会压缩 gzip_min_length 1k; # gzip 压缩级别,1-9,数字越大压缩的越好,也越占用CPU时间,后面会有详细说明 gzip_comp_level 1; # 进行压缩的文件类型。javascript有多种形式。其中的值能够在 mime.types 文件中找到。 gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png application/vnd.ms-fontobject font/ttf font/opentype font/x-woff image/svg+xml; # 是否在http header中添加Vary: Accept-Encoding,建议开启 gzip_vary on; # 禁用IE 6 gzip gzip_disable "MSIE [1-6]\."; # 设置压缩所须要的缓冲区大小 gzip_buffers 32 4k; # 设置gzip压缩针对的HTTP协议版本,没作负载的能够不用 # gzip_http_version 1.0; # 开启缓存 location ~* ^.+\.(ico|gif|jpg|jpeg|png)$ { access_log off; expires 2d; } location ~* ^.+\.(css|js|txt|xml|swf|wav)$ { access_log off; expires 24h; } location ~* ^.+\.(html|htm)$ { expires 1h; } location ~* ^.+\.(eot|ttf|otf|woff|svg)$ { access_log off; expires max; } # 格式 # expires 30s; # expires 30m; # expires 2h; # expires 30d;
1.nginx的response headers中的Content-Encoding是gzipjavascript
2.返回文件大小明显被压缩php
打开或关闭gzip
默认 off 关闭
代码块 http, server, location, if in locationcss
设置用于处理请求压缩的缓冲区数量和大小。好比32 4K表示按照内存页(one memory page)大小以4K为单位(即一个系统中内存页为4K),申请32倍的内存空间。建议此项不设置,使用默认值。html
Syntax: gzip_buffers number size; Default: gzip_buffers 32 4k|16 8k; Context: http, server, location
设置gzip压缩级别,级别越底压缩速度越快文件压缩比越小,反之速度越慢文件压缩比越大前端
Syntax: gzip_comp_level level; Default: gzip_comp_level 1; Context: http, server, location
不是压缩级别越高越好,其实gzip_comp_level 1的压缩能力已经够用了,后面级别越高,压缩的比例其实增加不大,反而很吃处理性能。 另外一方面,压缩必定要和静态资源缓存相结合,缓存压缩后的版本,不然每次都压缩高负载下服务器确定吃不住。
经过表达式,代表哪些UA头不使用gzip压缩java
Syntax: gzip_disable regex ...; Default: — Context: http, server, location This directive appeared in version 0.6.23.
当返回内容大于此值时才会使用gzip进行压缩,以K为单位,当值为0时,全部页面都进行压缩。nginx
Syntax: gzip_min_length length; Default: gzip_min_length 20; Context: http, server, location
用于识别http协议的版本,早期的浏览器不支持gzip压缩,用户会看到乱码,因此为了支持前期版本加了此选项。默认在http/1.0的协议下不开启gzip压缩。后端
Syntax: gzip_http_version 1.0 | 1.1; Default: gzip_http_version 1.1; Context: http, server, location
在应用服务器前,若是还有一层Nginx的集群做为负载均衡,在这一层上,若果没有开启gzip。 若是咱们使用了proxy_pass进行反向代理,那么nginx和后端的upstream server之间默认是用HTTP/1.0协议通讯的。 若是咱们的Cache Server也是nginx,而前端的nginx没有开启gzip。 同时,咱们后端的nginx上没有设置gzip_http_version为1.0,那么Cache的url将不会进行gzip压缩。
Nginx作为反向代理的时候启用:浏览器
1. off – 关闭全部的代理结果数据压缩 2. expired – 若是header中包含”Expires”头信息,启用压缩 3. no-cache – 若是header中包含”Cache-Control:no-cache”头信息,启用压缩 4. no-store – 若是header中包含”Cache-Control:no-store”头信息,启用压缩 5. private – 若是header中包含”Cache-Control:private”头信息,启用压缩 6. no_last_modified – 启用压缩,若是header中包含”Last_Modified”头信息,启用压缩 7. no_etag – 启用压缩,若是header中包含“ETag”头信息,启用压缩 8. auth – 启用压缩,若是header中包含“Authorization”头信息,启用压缩 9. any – 无条件压缩全部结果数据
Syntax: gzip_proxied off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any ...; Default: gzip_proxied off; Context: http, server, location
设置须要压缩的MIME类型,若是不在设置类型范围内的请求不进行压缩缓存
Syntax: gzip_types mime-type ...; Default: gzip_types text/html; Context: http, server, location
增长响应头”Vary: Accept-Encoding”
告诉接收方发送的数据通过了压缩处理,开启后的效果是在响应头部添加了Accept-Encoding:gzip,这对于自己不支持gzip压缩的客户端浏览器有用。
Syntax: gzip_vary on | off; Default: gzip_vary off; Context: http, server, location