使用mod_deflate模块javascript
优势:能够压缩页面,优化传输速度,节省带宽。css
缺点:会额外消耗CPU,同时可能有些较老的浏览器不支持压缩。html
NOTE:java
适用于适合压缩的资源,例如文本文件。正则表达式
配置说明:浏览器
一、首先须要启用mod_deflate模块,CentOS6中的httpd.conf配置文件里是默认启用了mod_deflate模块的。bash
若是被禁用,能够在http.conf中进行修改,去掉注释便可。服务器
二、调用DEFLATE输出过滤器
app
设置过滤器,并向过滤器添加明确说明哪些是能经过过滤,容许使用压缩功能的文件类型curl
SetOutputFilter DEFLATE # mod_deflate configuration # Restrict compression to there MIME types AddOutputFilterByType DEFLATE text/plain AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE application/xhtml+xml AddOutputFilterByType DEFLATE text/xml AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE application/x-javascript AddOutputFilterByType DEFLATE text/javasrcipt AddOutputFilterByType DEFLATE text/css # Level of compression (Highest 9 - lowest 1) DeflateCompressionLevel 9 //设定压缩比例,数值越高,压缩比例越大,耗时越慢 # Netscape 4.x has some problems. BrowserMatch ^Mozilla/4 gzip-only-text/html //基于正则表达式对浏览器类型和版本作匹配筛选 # Netscape 4.06-4.08 have some more problems BrowserMatch ^mozilla/4\.0[678] no-gzip # MSIE masquerades as Netsacpe,but it is fine BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
三、配置完成后,能够使用curl命令测试配置效果:
使用-I选项显示相应报文的首部信息
使用--compressed选项要求服务器端将报文压缩后发送
能够看到Content-Encoding:gzip
说明配置生效
写的比较潦草,若有遗漏错误和争议之处,欢迎你们的批评指正和讨论,谢谢。