转自:http://www.blogjava.net/site120/archive/2007/02/16/100073.html
背景:
HTTP 压缩可以大大提高浏览网站的速度,它的原理是,在客户端请求网页后,从服务器端将网页文件压缩,再下载到客户端,由客户端的浏览器负责解压缩并浏览。相对于普通的浏览过程HTML ,CSS,Javascript , Text ,它可以节省40%左右的流量。更为重要的是,它可以对动态生成的,包括CGI、PHP , JSP , ASP , Servlet,SHTML等输出的网页也能进行压缩,压缩效率惊人
一,对于Tomcat5.0以后的版本是支持对输出内容进行压缩的. 使用的是gzip压缩格式
下面是tomcat5.5.20 中的$tomcat_home$/conf/server.xml的原内容
1
<
Connector
port
="80"
maxHttpHeaderSize
="8192"
2
maxThreads
="150"
minSpareThreads
="25"
maxSpareThreads
="75"
3
enableLookups
="false"
redirectPort
="8443"
acceptCount
="100"
4
connectionTimeout
="20000"
disableUploadTimeout
="true"
URIEncoding
="utf-8"
/>
5
<!--
Note : To disable connection timeouts, set connectionTimeout value
6
to 0
-->
7
8
<!--
Note : To use gzip compression you could set the following properties :
9
10
compression="on"
11
compressionMinSize="2048"
12
noCompressionUserAgents="gozilla, traviata"
13
compressableMimeType="text/html,text/xml"
14
-->
从上面的
第8行内容可以看出,要使用gzip压缩功能,你可以在Connector实例中加上如下属性即可
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
2
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
3
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
4
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
5
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
6
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
7
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
8
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
9
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
10
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
11
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
12
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
13
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
14
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
1) compression="on" 打开压缩功能
2) compressionMinSize="2048" 启用压缩的输出内容大小,这里面默认为2KB
3) noCompressionUserAgents="gozilla, traviata" 对于以下的浏览器,不启用压缩
4) compressableMimeType="text/html,text/xml" 压缩类型
我这里的配置内容为:
1
<
Connector
port
="80"
maxHttpHeaderSize
="8192"
2
maxThreads
="150"
minSpareThreads
="25"
maxSpareThreads
="75"
3
enableLookups
="false"
redirectPort
="8443"
acceptCount
="100"
4
connectionTimeout
="20000"
disableUploadTimeout
="true"
URIEncoding
="utf-8"
5
compression
="on"
6
compressionMinSize
="2048"
7
noCompressionUserAgents
="gozilla, traviata"
8
compressableMimeType
="text/html,text/xml,text/javascript,text/css,text/plain"
/>
9
<!--
Note : To disable connection timeouts, set connectionTimeout value
10
to 0
-->
11
12
<!--
Note : To use gzip compression you could set the following properties :
13
14
compression="on"
15
compressionMinSize="2048"
16
noCompressionUserAgents="gozilla, traviata"
17
compressableMimeType="text/html,text/xml"
18
-->
19
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
2
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
3
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
4
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
5
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
6
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
7
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
8
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
9
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
10
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
11
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
12
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
13
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
14
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
15
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
16
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
17
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
18
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
19
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
二,对于Apache而言,有两种情况
1)针对Apache2.0之前的版本,它原本是不支持的,不过可以通过添加第三方的module_gzip模块来启用
2)针对Apache2.0及之后的版本,Apache提供支持, 不过不叫gzip,而叫mod_deflate
下面就对Apache2.0及之后的版本作一个说明
1) 去掉#LoadModule headers_module modules/mod_headers.so前面的注释#,
2) 添加LoadModule deflate_module modules/mod_deflate.so
3) 在VirtualHost中添加
1
<
Location
"/"
>
2
SetOutputFilter DEFLATE
3
BrowserMatch ^Mozilla/4 gzip-only-text/html
4
BrowserMatch ^Mozilla/4/.0[678] no-gzip
5
BrowserMatch /bMSIE !no-gzip !gzip-only-text/html
6
SetEnvIfNoCase Request_URI /.(?:gif|jpe?g|png)$ no-gzip dont-vary
7
Header append Vary User-Agent env=!dont-vary
8
</
Location
>
9
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
2
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
3
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
4
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
5
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
6
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
7
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
8
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
9
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
我这里面有一个完整的演示
1
# 加载deflate模块
2
LoadModule headers_module modules/mod_headers.so
3
LoadModule deflate_module modules/mod_deflate.so
4
<
VirtualHost
*:80
>
5
DocumentRoot f:/apacheTest
6
<
Location
"/"
>
7
SetOutputFilter DEFLATE
8
BrowserMatch ^Mozilla/4 gzip-only-text/html
9
BrowserMatch ^Mozilla/4/.0[678] no-gzip
10
BrowserMatch /bMSIE !no-gzip !gzip-only-text/html
11
SetEnvIfNoCase Request_URI /.(?:gif|jpe?g|png)$ no-gzip dont-vary
12
Header append Vary User-Agent env=!dont-vary
13
</
Location
>
14
</
VirtualHost
>
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
2
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
3
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
4
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
5
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
6
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
7
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
8
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
9
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
10
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
11
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
12
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
13
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
14
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)