背景:
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例項中加上如下屬性即可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 -->
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
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
二,對於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
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
我這裡面有一個完整的演示
1# 載入deflate模組
2LoadModule headers_module modules/mod_headers.so
3LoadModule 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>
2LoadModule headers_module modules/mod_headers.so
3LoadModule 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>