Forget is the first time to read the "high-performance Web site Construction Guide," the "Rules 4── compression Components" chapter, has been a muddle, today only came to realize, originally by reducing the size of the HTTP response to reduce response time applied to the Tomcat server is the case, The result was comforting and embarrassing. Gzip compression rate of up to 70%, which is to improve the performance of the web is the inverse of the sky, and today before I, but never know! Presumably a lot of Daniel has not bothered to tidy up such information, but for me, "like a blank paper, love is just beginning, I want to write too many words!" ”
First, the effect shows
For JS, CSS, html Three kinds of commonly used types, the high compression rate of people happy. Take HTML, 47.49KB size gzip 5.32KB After, obviously will greatly improve the transmission speed of the network, of course, compared to the project without gzip, use gzip to increase the pressure of server compression (CPU consumption), the pressure of client decompression, Specific consumption performance of the increase and consumption between the "energy conversion ratio" there is how much, I can not be informed, but if you are interested, you may see whether the mainstream website (Baidu) is using gzip.
Ii. How to use gzip
The principle of gzip work is simple, the HTTP request header adds a "accept-encoding:gzip" command to inform the server that gzip compression is required, and the server receives the request through the "Content-encoding:gzip" To inform the client of Gzip decompression.
The gzip request instructions send seems (this, I haven't figured out) is the default, open Firefox Firebug to observe, you can see the header of the request contains "accept-encoding:gzip, deflate (no gzip efficient, And not popular compression) "instructions.
The request does not need to be set up, but it is set up, for the Tomcat server, the following methods are found:
<Connector Executor="Tomcatthreadpool" Port="a" Protocol="http/1.1" ConnectionTimeout="20000" Redirectport="8443" uriencoding="UTF-8" Compression="on" compressionminsize="2048" nocompressionuseragents="Gozilla, Traviata" Compressablemimetype="Text/html,text/xml,application/javascript,text/css,text/plain,text/json"/>
The connector is configured as above in Tomcat's server.xml:
compression="on"Turn on compression
compressionMinSize="2048"Files larger than 2KB are compressed (the high-performance website Construction Guide recommends 1KB or more than 2KB for reasons unknown)
noCompressionUserAgents="gozilla, traviata", for both browsers, do not compress (I do not know what these two browsers are, Baidu did not find)
compressableMimeType="text/html,text/xml,application/javascript,text/css,text/plain,text/json"Show support for HTML, XML, JS, CSS, JSON, and other file format compression (plain is unformatted, but for what it is, I compare the concept of ambiguity).
Then restart Tomcat.
Third, in the end there is no gzip open
Now I know that there are two ways to do this:
- Java code testing (available when there is no extranet permission.) Tgzip.java (detailed reference to enable TOMCAT6 's hidden gzip compression feature), note the need to introduce Apache's Commons package! )。
- http://gzip.zzbaike.com/, the image of the beginning of the article comes from this website (no extranet permission, not available, this kind of practice does not need to introduce).
Iv. Cache-control
The "Rules 4── compression Components" chapter of the high-performance website building guide concludes with some questions about the "Edge situation" (I'm not going to talk about the problem), and one of the solutions is
Cache-control:private header to disable proxy caching.
Here, let's see how to set it (from StackOverflow)!
①, Cachecontrolfilter.java
PackageCom.honzh.common.filter;ImportJava.io.IOException;ImportJavax.servlet.Filter;ImportJavax.servlet.FilterChain;ImportJavax.servlet.FilterConfig;ImportJavax.servlet.ServletException;ImportJavax.servlet.ServletRequest;ImportJavax.servlet.ServletResponse;ImportJavax.servlet.http.HttpServletResponse; Public class cachecontrolfilter implements Filter { Public void DoFilter(ServletRequest request, servletresponse response, Filterchain chain)throwsIOException, servletexception {HttpServletResponse resp = (httpservletresponse) response;//Resp.setheader ("Expires", "Tue, Jul 2001 06:00:00 GMT");//Resp.setdateheader ("Last-modified", New Date (). GetTime ());//Resp.setheader ("Cache-control", "No-store, No-cache, Must-revalidate, max-age=0, post-check=0, pre-check=0"); /c2>Resp.setheader ("Cache-control","max-age=0, Private");//Resp.setheader ("Pragma", "No-cache");Chain.dofilter (request, response); }@Override Public void Destroy() { }@Override Public void Init(Filterconfig arg0)throwsServletexception {}}
Add above filer in ②, Web. Xml.
<filter> <filter-name>Cachecontrolfilter</filter-name> <filter-class>Com.honzh.common.filter.CacheControlFilter</filter-class> </filter> <filter-mapping> <filter-name>Cachecontrolfilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
V. Differences in requests for GZIP and non-gzip
Enjoy the thrill of Web performance with such a little gesture optimization!
Gzip compressed Tomcat server Response pack for significantly improved Web performance