分享好友 系统运维首页 频道列表

Tomcat配置gzip压缩提高浏览网站的速度

Tomcat  2015-06-28 13:440

HTTP 压缩可以大大提高浏览网站的速度,它的原理是,在客户端请求网 页后,从服务器端将网页文件压缩,再下载到客户端,由客户端的浏览器负责解 压缩并浏览。相对于普通的浏览过程HTML ,CSS,Javascript , Text ,它可以节省40%左右的流量。更为重要的是,它可以对动态生成的,包括CGI、PHP , JSP , ASP , Servlet,SHTML等输出的网页也能进行压缩,压缩效率惊人

一 对于Tomcat5.0以后的版本是支持对输出内容进行压缩的使用的是gzip压缩格式

下 面是tomcat5.5.20 中的$tomcat_home$/conf/server.xml的原内容
< Connector port ="80" maxHttpHeaderSize ="8192"
maxThreads ="150" minSpareThreads ="25" maxSpareThreads ="75"
enableLookups ="false" redirectPort ="8443" acceptCount ="100"
connectionTimeout ="20000" disableUploadTimeout ="true" URIEncoding ="utf-8" />
<!-- Note : To disable connection timeouts, set connectionTimeout value
to 0 -->

<!-- Note : To use gzip compression you could set the following properties :

compression="on"
compressionMinSize="2048"
noCompressionUserAgents="gozilla, traviata"
compressableMimeType="text/html,text/xml"

-->从上面的第 8行内容可以看出,要使用gzip压缩功能,你可以在Connector实例中加上如下 属性即可

1) compression="on" 打开压缩功能
2) compressionMinSize="2048" 启用压缩的输出内容大小,这里面默认为2KB
3) noCompressionUserAgents="gozilla, traviata" 对于以下的浏览器,不启用压缩
4) compressableMimeType="text/html,text/xml" 压缩类型(默认为text/html,text/xml,text/plain)

我 这里的配置内容为:

<Connector port="80" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="utf-8"
compression="on" 
compressionMinSize="2048" 
noCompressionUserAgents="gozilla, traviata" 
compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain" />
<!-- Note : To disable connection timeouts, set connectionTimeout value
to 0 -->

<!-- Note : To use gzip compression you could set the following properties :

compression="on" 
compressionMinSize="2048" 
noCompressionUserAgents="gozilla, traviata" 
compressableMimeType="text/html,text/xml"
-->

一旦启用了这个压缩功能后,我们怎么来测试压缩是否有效呢?首先Tomcat是根据浏览器请求头中的accept-encoding来判断浏览器是否支持 压缩功能,如果这个值包含有gzip,就表明浏览器支持gzip压缩内容的浏览,所以我们可以用httpclient来写一个这样的简单测试程序

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;

public class HttpTester {

public static void main(String[] args) throws Exception{
HttpClient http = new HttpClient();
GetMethod get = new GetMethod("http://www.dlog.cn/js/prototype.js");
try{
get.addRequestHeader("accept-encoding", "gzip,deflate");
get.addRequestHeader("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Alexa Toolbar; Maxthon 2.0)");
int er = http.executeMethod(get);
if(er==200){
System.out.println(get.getResponseContentLength());
String html = get.getResponseBodyAsString();
System.out.println(html);
System.out.println(html.getBytes().length);
}
}finally{
get.releaseConnection();
}
}

}

执行这个测试程序,看看它所输出的是什么内容,如果输出的是一些 乱码,以及打印内容的长度远小于实际的长度,那么恭喜你,你的配置生效了,你会发现你网站的浏览速度比以前快多了。

查看更多关于【Tomcat】的文章

展开全文
相关推荐
反对 0
举报 0
评论 0
图文资讯
热门推荐
优选好物
更多热点专题
更多推荐文章
装tomcat和nginx心得
开机启动tomcat1:在/etc/rc.d/init.d目录下生成一个文件tomcat80802:在文件里添加如下内#!/bin/bash#2345 linux运行级别#10开机启动优先级,数值越大越排在前面,最大值100#90关机优先级#chkconfig:2345 10 90#description: tomcat8080 start....start(){ec

0评论2023-02-13333

apache2 tomcat https配置-被忽悠进了CentOS 6
  因为需要让ios应用可以绕过appstore,要找个https的地方放ipa,决定使用已有http server。  首先需要说明的是,apache可以通过反向代理方式将用户的https分发到tomcat的http上,因此只需要配置apache的https即可。但我这里还是把两个服务器的https都配

0评论2023-02-10411

windows下apache tomcat整合
准备工作:1、 httpd-2.2.22-win32-x86-openssl-0.9.8t.msi下载地址:http://apache.etoak.com//httpd/binaries/win32/httpd-2.2.22-win32-x86-openssl-0.9.8t.msi2、 apache-tomcat-6.0.20.zip(免安装版-绿色版)下载地址:http://archive.apache.org/dist/

0评论2023-02-10918

Eclipse添加tomcat出现 The Apache Tomcat installation at this directory is version 8.5.6. A Tomcat 8.0 i
打开tomcat安装目录:apache-tomcat-8.5.6\lib 找到catalina.jar用解压缩工具打开 org/apache/catalina/util/ServerInfo.properties将:     server.info=Apache Tomcat/8.5.6  server.number=8.5.6.0  server.built=Oct 6 2016 20:15:31 UTC改为:

0评论2023-02-10323

An incompatible version [1.2.10] of the APR based Apache Tomcat Native library is installed, while T
 这个链接的博主写的很详细,直接推荐:https://blog.csdn.net/zhoukikoo/article/details/80532483

0评论2023-02-10809

tomcat启动提示java.lang.UnsatisfiedLinkError: D:\soft\devTool\apache-tomcat-7.0.57\bin\tcnative-1.dll: C
https://blog.csdn.net/a274360781/article/details/52411984

0评论2023-02-10643

【漏洞预警】Apache Tomcat Session 反序列化代码执行漏洞修复方案
Tomcat Session 漏洞https://blog.csdn.net/xia296/article/details/106249137/ 漏洞描述影响版本安全版本安全建议操作步骤下载你要升级的Tomcat 10.0.0-M5版本:备份旧的tomcat(这部分非必须,根据自己情况而定)开始安装新版本禁止使用Session持久化功能F

0评论2023-02-10690

The APR based Apache Tomcat Native library 异常解决办法
tomat在linux服务器上启动报The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/local/jdk1.6.0_26/jre/lib/i386/server:/usr/local/jdk1.6.0_26/jre/l

0评论2023-02-10585

nginx+tomcat反向代理下使用tomcat-redis-session-manager进行session共享中值得注意的一个问题
公司目前项目使用nginx反向代理+多个tomcat进行负载均衡,之前使用ip_hash策略进行session控制。近期有考虑不再使用ip_hash策略,所以需要进行session共享。根据项目实际情况,拟考虑使用开发配置比较简单,应用比较广泛的tomcat-redis-session-manager方式进

0评论2023-02-10303

tomcat和nginx介绍
tomcat为什么需要装java环境?因为tomcat是用java写的, 所以运行需要JRE,就是JAVA运行时刻环境,所以必须通过安装JDK来得到这个运行环境,不装JDK装JRE也行sun的网站上有下载。但是JRE只是运行时刻化境,不能编译JAVA源程序。 Tomcat是什么?有什么作用?一

0评论2023-02-10428

Nginx+Tomcat+Https 服务器负载均衡配置
这篇过气了!重新补一个:http://www.cnblogs.com/hackyo/p/6809773.html 由于需要,得搭建个nginx+tomcat+https的服务器,搜了搜网上的发现总是有错,现在整理了些有用的,备忘。环境:Centos6.5、JDK1.8、Tomcat8.5、Nginx1.10.2准备材料:1.JDK1.8安装包j

0评论2023-02-10700

更多推荐