什么是 gzip?
GZIP 最早由 Jean-loup Gailly 和 Mark Adle r创建,用于 UNIX 系统的文件压缩,它是网站压缩加速的一种技术,对于开启后可以加快我们网站的打开速度,原理是经过服务器压缩,客户端浏览器快速解压的原理,可以大大减少了网站的流量。
开启以后有什么好处?
提速!对于应答的请求进行压缩,使大小减少 40% 左右,目前浏览器几乎都支持 gzip 的解析,传输减少了,速度就提高了。
开启以后有什么问题?
压缩需要消耗服务器 cpu,可能会多服务器 web server 造成一定的负担,所以需要控制压缩的级别。
如何开启 gzip 压缩?
nginx:
打开 nginx.conf, 把 Gzip on 的注释去掉,下面是网上找的一个 demo:
Gzip on;
gzip_min_length 1024;
gzip_buffers 48k;
gzip_types text/plain application/x-javascript text/css application/xml;
apache:
在 httpd.conf 中,将下面内容解注释:
LoadModule deflate_module modules/mod_deflate.so
LoadModule headers_module modules/mod_headers.so
下面是网上的一个配置 demo:
SetOutputFilter DEFLATE
SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI .(?:pdf|mov|avi|mp3|mp4|rm)$ no-gzip dont-vary
DeflateCompressionLevel 6
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/x-httpd-php
AddOutputFilter DEFLATE js css