龙空技术网

web性能优化--用gzip压缩资源文件

进击的IT程序员 59

前言:

此刻我们对“php压缩css文件”大约比较关注,同学们都需要了解一些“php压缩css文件”的相关资讯。那么小编在网络上收集了一些有关“php压缩css文件””的相关文章,希望小伙伴们能喜欢,各位老铁们一起来了解一下吧!

#一、gzip压缩技术

gzip(GNU- ZIP)是一种压缩技术。经过gzip压缩后页面大小可以变为原来的30%甚至更小,这样,用户浏览页面的时候速度会快得多。gzip的压缩页面需要浏览器和服务器双方都支持,实际上就是服务器端压缩,传到浏览器后浏览器解压并解析。浏览器那里不需要我们担心,因为目前的大多数浏览器都支持解析gzip压缩过的资源文件。在实际的应用中我们发现压缩的比率往往在3到10倍,也就是本来50k大小的页面,采用压缩后实际传输的内容大小只有5至15k大小,这可以大大节省服务器的网络带宽,同时如果应用程序的响应足够快时,网站的速度瓶颈就转到了网络的传输速度上,因此内容压缩后就可以大大的提升页面的浏览速度。

实现gzip压缩的方式有多种,比如:nginx、tomcat、java等,选用其中一种即可。

#二、nginx启用gzip

Nginx的压缩输出有一组gzip压缩指令来实现。相关指令位于http{….}两个大括号之间,如下:

#打开gzip压缩

gzip on;

#不压缩临界值,大于1K的才压缩,一般不用改

gzip_min_length 1k;

#设置系统获取几个单位的缓存用于存储gzip的压缩结果数据流,这里设置以16k为单位的4倍申请内存

gzip_buffers 4 16k;

#默认为http 1.1,现在99.99%的浏览器基本上都支持gzip解压了,所有无需设置此项

#gzip_http_version 1.0;

#gzip压缩比,1 最小处理速度最快,9 最大但处理最慢(传输快但比较消耗cpu)

gzip_comp_level 2;

#要压缩的文件类型,注意"text/html"类型无论是否指定总是会被压缩的

gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/javascript application/x-httpd-php image/jpeg image/gif image/png;

#on的话会在Header里增加"Vary: Accept-Encoding",给代理服务器用的,有的浏览器支持压缩,有的不支持,所以避免浪费不支持的也压缩,所以根据客户端的HTTP头来判断,是否需要压缩

#我这里的浏览器肯定支持gzip压缩,所以就不开启此功能了

gzip_vary off;

#IE6对Gzip不怎么友好,不给它Gzip压缩了

gzip_disable "MSIE [1-6]\.";

#三、tomcat启用gzip

目前大多数主流WEB中间件都支持GZIP压缩、下面以Tomcat 为例进行说明:

找到Tomcat 目录下的conf下的server.xml,并找到如下信息:

<Connector port = "8080" maxHttpHeaderSize = "8192" maxThreads = "150" minSpareThreads = "25"

maxSpareThreads = "75" enableLookups = "false" redirectPort = "8443" acceptCount = "100"

connectionTimeout = "20000" disableUploadTimeout = "true"

将它改成如下的形式(其实在上面代码的下面已经有了,将他们打开而已。):

<Connector port="8080" maxHttpHeaderSize="8192" maxThreads="150" minSpareThreads="25"

maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100"

connectionTimeout="20000" disableUploadTimeout="true"

compression="on" compressionMinSize="2048" noCompressionUserAgents="gozilla, traviata"

compressableMimeType="text/html,text/xml" >

这样,就能够对html和xml进行压缩了,如果要压缩css 和 js,那么需要将

compressableMimeType=”text/html,text/xml”加入css和js:

<Connector port="8080" ......... compressableMimeType="text/html,text/xml,text/css,text/javascript" >

一般文本类型的静态文件可以通过这种方式压缩后传输、提高传输效率。

#四、java服务器启用gzip

java本身可以通过过滤器filter实现gzip压缩。下面提供一个gzip工具类:

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.util.zip.GZIPInputStream;

import java.util.zip.GZIPOutputStream;

public class GZIPUtils {

public static final String GZIP_ENCODE_UTF_8 = "UTF-8";

public static final String GZIP_ENCODE_ISO_8859_1 = "ISO-8859-1";

public static byte[] compress(String str, String encoding) {

if (str == null || str.length() == 0) {

return null;

}

ByteArrayOutputStream out = new ByteArrayOutputStream();

GZIPOutputStream gzip;

try {

gzip = new GZIPOutputStream(out);

gzip.write(str.getBytes(encoding));

gzip.close();

} catch ( Exception e) {

e.printStackTrace();

}

return out.toByteArray();

}

public static byte[] compress(String str) throws IOException {

return compress(str, GZIP_ENCODE_UTF_8);

}

public static byte[] uncompress(byte[] bytes) {

if (bytes == null || bytes.length == 0) {

return null;

}

ByteArrayOutputStream out = new ByteArrayOutputStream();

ByteArrayInputStream in = new ByteArrayInputStream(bytes);

try {

GZIPInputStream ungzip = new GZIPInputStream(in);

byte[] buffer = new byte[256];

int n;

while ((n = ungzip.read(buffer)) >= 0) {

out.write(buffer, 0, n);

}

} catch (Exception e) {

e.printStackTrace();

}

return out.toByteArray();

}

public static String uncompressToString(byte[] bytes, String encoding) {

if (bytes == null || bytes.length == 0) {

return null;

}

ByteArrayOutputStream out = new ByteArrayOutputStream();

ByteArrayInputStream in = new ByteArrayInputStream(bytes);

try {

GZIPInputStream ungzip = new GZIPInputStream(in);

byte[] buffer = new byte[256];

int n;

while ((n = ungzip.read(buffer)) >= 0) {

out.write(buffer, 0, n);

}

return out.toString(encoding);

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

public static String uncompressToString(byte[] bytes) {

return uncompressToString(bytes, GZIP_ENCODE_UTF_8);

}

public static void main(String[] args) throws IOException {

String s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";

System.out.println("字符串长度:"+s.length());

System.out.println("压缩后::"+compress(s).length);

System.out.println("解压后:"+uncompress(compress(s)).length);

System.out.println("解压字符串后::"+uncompressToString(compress(s)).length());

}

}

#五、压缩效果

压缩前:

压缩后:

显然压缩后资源文件变得小了很多,加载速度也快了不少。可见,gzip压缩是页面性能优化的一种有效方式。

欢迎各位小伙伴批评指正,也欢迎私信我探讨交流哈,还有免费的学习资料赠送

标签: #php压缩css文件 #gzip解压php