When we use Tomcat to optimize the configuration, we will start Tomcat's Gzip compression function, which is configured 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" />
That is, add the following content to the connector tag in apache-tomcat-6\conf\
<!-- 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" Turn on the compression function
compressionMinSize="2048" Enables the compressed output content size, and will be compressed only when the size of the compressed object is >= this value. The default is 2KB here
noCompressionUserAgents="gozilla, traviata" No compression 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 the js file becomes application/JavaScript. The specific type defined by tomcat7 can be found in:conf/ file.
My Tomcat6 is still text/javascript
Tomcat7 js file mimetype type becomes application/javascript
Pay attention to it yourself. If the error is not used to compress it.
So how do we test that the configured Gzip compression function has come into effect?
The answer is: use apache HttpClient to access a static resource in the project (such as a js file), and then print the requested resource content or resource ContentLength. If the printed resource content is garbled or ContentLength is -1, it means that gzip has taken effect.
import ; import ; /** * @ClassName: * @Description: TODO (describe what the file does in one sentence) * * @author Administrator * @E-mail 809044093@ * @version V1.0 * @Date 2014-3-27 09:07:00 AM */ 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/"); try{ ("accept-encoding", "gzip,deflate"); ("user-agent", "Mozilla/5.0 (compatible; MSIE 6.0; Windows NT 5.0; Alexa Toolbar; Maxthon 2.0)"); int er = (get); if(er==200){ (()); String html = (); (html); (().length); } }finally{ (); } } }
The console result is garbled
It means that the information of the compressed website is successfully configured, and this method may cause some loss to the server CPU.
Apache enables Gzip compression configuration:
Remove the following two comments#
LoadModule deflate_module modules/mod_deflate.so LoadModule headers_module modules/mod_headers.so
Join at last
#apache Gzip <Location /> # Insert filter SetOutputFilter DEFLATE # Netscape 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>
After the setup is complete, access/Gzips/Enter your website domain name to detect compression.