How do I verify that the Tomcat Gzip configuration works
- 2020-06-12 11:13:02
- OfStack
When we use Tomcat to optimize the configuration, we always start Tomcat's Gzip compression. The configuration is as follows:
<Connector port="9080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8" useBodyEncodingForURI="true"
compression="on"
compressionMinSize="2048"
noCompressionUserAgents="gozilla, traviata"
compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain,image/gif,image/jpg"
/>
Add the following to the connector tag in ES7en-ES8en-6 \conf\ server.xml
<!-- Note : To use Gzip compression you could set the following properties :
compression="on"
compressionMinSize="2048"
noCompressionUserAgents="gozilla, traviata"
compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain,image/gif,image/jpg"
-->
Parameter description:
compression="on" turns on compression
compressionMinSize="2048" enables compressed output content size when compressed object size
>
= this value is compressed, which defaults to 2KB
noCompressionUserAgents="gozilla, traviata" Compression is not enabled for the following browsers
compressableMimeType = "text/html, text/xml, text/javascript, text/css, text/plain, image/gif, image/jpg" compression type
Note: after tomcat7, the mimetype type of js file becomes application/JavaScript. The specific type defined by tomcat7 can be found in: conf/ web.xml file.
My Tomcat6 is still text/javascript
The mimetype type of Tomcat7 js file is changed to application/javascript
I will not compress if I mismatch
So how do we test that the configured Gzip compression works?
The answer is: use apache HttpClient to access a static resource (such as an js file) in the tomcat load project, and then print the requested resource content or resource ContentLength. If the printed resource content is a gibbe or ContentLength is -1, then gzip is valid.
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
/**
* @ClassName: GzipTest.java
* @Description: TODO( with 1 What does the document do in a sentence )
*
* @author Administrator
* @E-mail 809044093@qq.com
* @version V1.0
* @Date 2014-3-27 In the morning 09:07:00
*/
public class GzipTest {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
HttpClient http = new HttpClient();
GetMethod get = new GetMethod("http://127.0.0.1:9080/membercms/style/shop/js/base.js");
try{
get.addRequestHeader("accept-encoding", "gzip,deflate");
get.addRequestHeader("user-agent", "Mozilla/5.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();
}
}
}
The console results are garbled
This method may cause some loss to server cpu.
Apache Enable Gzip compression configuration:
Remove the following two comments #
LoadModule deflate_module modules/mod_deflate.so
LoadModule headers_module modules/mod_headers.so
Join at httpd.conf
#apache Gzip
<Location />
# Insert filter
SetOutputFilter DEFLATE
# Netscape 4.x has some problems...
BrowserMatch ^Mozilla/4 gzip-only-text/html
# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4\.0[678] no-gzip
# MSIE masquerades as Netscape, but it is fine
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
# Don't compress images
SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI .(?:pdf|doc|avi|mov|mp3|rm)$ no-gzip dont-vary
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/js
# Make sure proxies don't deliver the wrong content
#Header append Vary User-Agent env=!dont-vary
</Location>
Set after completion of access http: / / tool chinaz. com Gzips/enter your website domain name compression conditions.