电竞比分网-中国电竞赛事及体育赛事平台

分享

tomcat 中的gzip壓縮配置

 小朋 2007-07-19
#

這兩篇文章主要介紹在apache http server下如何使用mod_deflate模塊來(lái)壓縮http響應(yīng)內(nèi)容,最大限度的減小網(wǎng)絡(luò)流量,然而當(dāng)我們僅僅是使用tomcat服務(wù)器時(shí)候,更多的文章是通過(guò)自行編寫一些代碼來(lái)實(shí)現(xiàn)輸出內(nèi)容的壓縮,其實(shí)tomcat本身在5.0版本以后是支持內(nèi)容壓縮的,它使用的是gzip的壓縮格式,我們先來(lái)看 Tomcat文檔中對(duì)下面兩個(gè)配置的注解(紅色粗體字部分)

compressableMimeType

The value is a comma separated list of MIME types for which HTTP compression may be used. The default value is text/html,text/xml,text/plain.

compression

The Connector may use HTTP/1.1 GZIP compression in an attempt to save server bandwidth. The acceptable values for the parameter is "off" (disable compression), "on" (allow compression, which causes text data to be compressed), "force" (forces compression in all cases), or a numerical integer value (which is equivalent to "on", but specifies the minimum amount of data before the output is compressed). If the content-length is not known and compression is set to "on" or more aggressive, the output will also be compressed. If not specified, this attribute is set to "off".

這兩個(gè)配置是在servere.xml中的Connector部分,第一個(gè)配置是指定Tomcat壓縮哪些內(nèi)容,第二個(gè)配置是指示Tomcat是否啟用壓縮,默認(rèn)是關(guān)閉的。所以假設(shè)我們要讓Tomcat在默認(rèn)的8080端口上的輸出內(nèi)容進(jìn)行壓縮,我們的配置應(yīng)該是:

    <Connector port="8080" protocol="HTTP/1.1"
               maxThreads="150" connectionTimeout="20000"
               redirectPort="8443" compression="on"/>

一旦啟用了這個(gè)壓縮功能后,我們?cè)趺磥?lái)測(cè)試壓縮是否有效呢?首先Tomcat是根據(jù)瀏覽器請(qǐng)求頭中的accept-encoding來(lái)判斷瀏覽器是否支持壓縮功能,如果這個(gè)值包含有g(shù)zip,就表明瀏覽器支持gzip壓縮內(nèi)容的瀏覽,所以我們可以用httpclient來(lái)寫一個(gè)這樣的簡(jiǎn)單測(cè)試程序

package com.liusoft.dlog4j.test;

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

/**
 * HTTP客戶端測(cè)試類
 * @author liudong
 */
public class HttpTester {

 /**
  * @param args
  */
 public static void main(String[] args) throws Exception{
  HttpClient http = new HttpClient(); 
  GetMethod get = new GetMethod("http://www./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();
  }
 }

}

執(zhí)行這個(gè)測(cè)試程序,看看它所輸出的是什么內(nèi)容,如果輸出的是一些亂碼,以及打印內(nèi)容的長(zhǎng)度遠(yuǎn)小于實(shí)際的長(zhǎng)度,那么恭喜你,你的配置生效了,你會(huì)發(fā)現(xiàn)你網(wǎng)站的瀏覽速度比以前快多了。

另外你最好對(duì)網(wǎng)站所用的javascript和css也進(jìn)行壓縮:)

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多