启用GZIP压缩优化网站

[b][color=red][size=x-large]来源别人的资料,仅用于作为个人笔记[/size][/color]
[/b]
GZIP最早由Jean-loup Gailly和Mark Adler创建,用于UNⅨ系统的文件压缩。我们在Linux中经常会用到后缀为.gz的文件,它们就是GZIP格式的。现今已经成为Internet 上使用非常普遍的一种数据压缩格式,或者说一种文件格式。
  HTTP协议上的GZIP编码是一种用来改进WEB应用程序性能的技术。大流量的WEB站点常常使用GZIP压缩技术来让用户感受更快的速度。这一般是指WWW服务器中安装的一个功能,当有人来访问这个服务器中的网站时,服务器中的这个功能就将网页内容压缩后传输到来访的电脑浏览器中显示出来.一般对纯文本内容可压缩到原大小的40%.这样传输就快了,效果就是你点击网址后会很快的显示出来.当然这也会增加服务器的负载. 一般服务器中都安装有这个功能模块的.
 1、Apache启用gzip
  如果要开启gzip的话,一定要打开下面二个模块.
  LoadModule headers_module modules/mod_headers. so
  LoadModule deflate_module modules/mod_deflate. so
  设置压缩比率,取值范围在 1(最低) 到 9(最高)之间,不建议设置太高,虽然有很高的压缩率,但是占用更多的CPU资源.
  DeflateCompressionLevel 3
  AddOutputFilter DEFLATE html xml php js css
  <Location />
  SetOutputFilter DEFLATE
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4\.0[678] no-gzip
  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
  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
  Header append Vary User-Agent env=!dont-vary #对代理的设置
  </Location>

 把mod_gzip放到你的apache的源代码目录下,新建一个mod_gzip目录如果需要补丁(针对1.3.17. la版) 还需运行:
  patch mod_gizp.c
  按你需要,在配置中选择动态DSO或静态编译进apache系统。如何处理在README中讲得很清楚,如-add-module=mod_gzip.c,make,make install等等。这里不多讲。
  把下列配置加入httpd.conf尾部。
  # MOD_GZIP configuration
  mod_gzip_on Yes
  mod_gzip_minimum_file_size 1002
  mod_gzip_maximum_file_size 0
  mod_gzip_maximum_inmem_size 60000
  mod_gzip_item_include mime "application/x-httpd-php"
  mod_gzip_item_include mime text/*
  mod_gzip_item_include mime "httpd/unix-directory"
  mod_gzip_dechunk Yes
  mod_gzip_temp_dir "/tmp"
  mod_gzip_keep_workfiles No
  mod_gzip_item_include file ".php3$"
  mod_gzip_item_include file ".txt$"
  mod_gzip_item_include file ".html$"
  mod_gzip_item_exclude file ".css$"
  mod_gzip_item_exclude file ".js$"
  在保存修改后运行
  …/bin/apachectl configtest确保配置修改无误。
  然后用 apachectl restart 指令重起服务。


===========================================================》
对接口数据进行GZIP解压。。

public class SohuTest {

/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
}

@Test
public void test() {

URL url = null;
try {
DataOutputStream output = new DataOutputStream(new FileOutputStream("d://sohu.txt"));
String myurl = "第三方接口地址,服务端启用了Gzip";
url = new URL(myurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
conn.setRequestProperty("Accept-Encoding", "gzip,deflate");
DataInputStream input = new DataInputStream(new GZIPInputStream(conn.getInputStream()));
byte[] buffer = new byte[1024 * 8];
int count = 0;
while ((count = input.read(buffer)) > 0) {
output.write(buffer, 0, count);
}
System.out.println("=====>完毕");
} catch (Exception e) {
e.printStackTrace();
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值