Tomcat is configured with gzip compression to speed up your web browsing

  • 2020-05-12 06:32:49
  • OfStack

HTTP compression can greatly improve the speed of browsing websites. The principle of HTTP compression is that after the client side requests the web page, the web file is compressed from the server side, and then downloaded to the client side. The browser of the client side is responsible for decompressing and browsing. HTML,CSS,Javascript, Text, it can save about 40% of the traffic compared to the normal browsing process HTML,CSS,Javascript, Text. More importantly, it can compress dynamically generated web pages, including CGI, PHP, JSP, ASP, Servlet,SHTML and so on. The compression efficiency is amazing

1 for versions after Tomcat 5.0, compression of output content is supported using the gzip compression format

Below is the original content of $tomcat_home$/conf/ server.xml in tomcat5.5.20
< 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"

-- > As you can see from line 8 above, to use gzip compression, you can simply add the following properties to the Connector instance

1) compression="on" turns on the compression function
2) compressionMinSize="2048" with compressed output size enabled, which defaults to 2KB
3) noCompressionUserAgents="gozilla, traviata" compression is not enabled for the following browsers
4) compressableMimeType = "text/html, text/xml" compression type (the default is text/html, text/xml, text/plain)

My configuration content here is:


<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"
-->

Once this compression is enabled, how can we test whether the compression is effective? First of all, Tomcat determines whether the browser supports compression function according to accept-encoding in the browser request header. If this value contains gzip, it means that the browser supports the browsing of gzip compressed content. Therefore, we can use httpclient to write a simple test program like this


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();
}
}

}

Run this test to see what it prints, and if it prints 1 garbled code and the length of the printed content is much less than the actual length, congratulations, your configuration is working and you will find your site much faster than before.


Related articles: