Tomcat enables gzip compression to improve Web performance

Source: Internet
Author: User
Tags apache tomcat

First, preface

recently made a project, encountered a problem: the server returned to the client's JSON data volume is too large (about 65M), the client load more than 1 minutes to render complete, time consuming traffic, the user experience is extremely bad. Later on-line search optimization method, is HTTP compression.

HTTP compression can greatly improve the speed of browsing the site, it is the principle that after the client requests the server corresponding resources, from the server side of the resource file compression, and then output to the client, by the client's browser is responsible for extracting and browsing. That is, reduce the response time by reducing the size of the HTTP response . Relative to the normal browsing process HTML, Css,javascript, Text, it can save about 40% of the traffic. More importantly, it can be dynamically generated, including CGI, PHP, JSP, ASP, servlet,shtml and other output of the Web page can also be compressed, compression efficiency is very high. Gzip itself is a network flow compression algorithm, and the application is quite extensive. This article is to configure GZIP compression for Apache Tomcat 8.0.47. The browser uses Mozilla Firefox 35.0.1, debugging with its own FirebuG, the following and network-related from the FirebuG -Console .

  

ii. Introduction of Gzip compression

1. The HTTP protocol supports the GZIP compression mechanism, also known as protocol compression. HTTP gzip Compression is a protocol that is adhered to by Web servers and browsers, which means that both Web servers and browsers must comply. The current mainstream server and browser support gzip compression technology. including IE, FireFox, Opera, etc., the server has Tomcat, Apache, IIS and so on.

2. GZIP is mainly used to compress html,css,javascript, such as static text files, also support for dynamically generated, including CGI, PHP, JSP, ASP, Servlet, sHTML and other output pages can also be compressed.

3. GZIP Compression ratio is usually between 3~10 times, which can greatly save the network bandwidth of the server, greatly improve the browser's browsing speed.

4. GZIP is a data compression format that, by default, only uses the DEFLATE algorithm to compress the data section; Deflate is a compression algorithm, which is an enhancement of Huffman coding .

5. Protocol compression is based on the HTTP protocol compression, does not require the programmer to compress, unzip the code, but the compression process to the Web server, the decompression process to the client. If the client is a browser that supports gzip compression, then the decompression process does not require the programmer to participate, the browser will be automatically decompressed according to certain rules, if the client is httpclient, then it needs to be manually gzip decoded.

6. Compression process: The client sends an HTTP request, if the header of the request is carried Accept-encoding: gzip, deflate (now the browser is generally the default), so the browser means : The server needs GZIP compression, and then see if the type of response content satisfies the type of compression required for the server configuration, and if so, then Before the Web server transmits the response content, the response content is compressed and Content-encodinG gzip is added to the response header, and if it does not, it is not compressed and is returned directly.

7. Decompression process: (browser) The client receives a response, if the response header contains Content-encoding GZIP, then the browser will automatically gzip the response content to be extracted and then rendered on the page. If it is not included, it is rendered directly on the page.

8. Shortcomings ofGZIP . For projects that do not have gzip , using gzip increases the pressure on the server to compress (CPU consumption), the pressure of the client decompression, and hence the need for higher server configuration . In addition, compression also takes time, want to occupy a smaller space, get high compression ratio, must sacrifice a long time; conversely, if the time is more valuable, demand fast, then the resulting compression ratio must be small, of course, will occupy more space (Compression ratio = The original content size/compression size, the greater the compression ratio, the smaller the compressed space-occupying compressed packet), this is the contradiction between physical space and time.

Three in Tomcat Configuration Method

Version requirements: Tomcat5.0 to the top. to Modify the%tomcat_home%/conf/server.xml, the revision node is as follows:

1 <connector port= "8080"2   protocol= "http/1.1"3   connectiontimeout= "20000" 4   Redirectport= "8443"    5   6   7   nocompressionuseragents= "Gozilla, Traviata "   8   compressablemimetype=" Text/html,text/xml,text/javascript,9 Application/javascript,text/css,text/plain,text/json "/>

Parameter description:

1, compression= "on" to open the compression. Optional value: "On" is on, "off" is off, "force" is turned on in any case.

2, compressionminsize= "2048" files larger than 2KB compression. Used to specify the minimum data size for compression, Unit B, default 2048B. Note the size of this value, if the configuration is not reasonable, the result is small file compression has become larger, not achieve the desired effect.

3, Nocompressionuseragents= "gOzilla, Traviata", for these two browsers, do not compress (I do not know what these two browsers are, Baidu did not find), its value is Regular expressions, the matching UA will not be compressed, default NULL.

4, compressablemimetype= "Text/html,text/xml,application/javascript,text/css,text/plain,text/json" will be compressed MIME type list, Multiple commas, indicating 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). Compressablemimetype is important to tell Tomcat which file to compress, and if the type specifies an error, it must not be compressed. So, how do you know what type of file to compress? This can be found in the following way.

  

Four Check if the configuration is successful

After the modification, restart the next Tomcat , and finally to detect the site: detection site query effect

Five Common errors (no effect after configuration)

You can use the following steps to troubleshoot:

1. The configuration parameters in Tomcat are misplaced. Note that the configuration parameters should be written in the zone a rather than zone B, which is protocol= "http/1.1" in the connector.

  

2, the response data is not the type of compressablemimetype parameter configuration . I ran into this pit, and the front and back end of our project was JSON. So I first thought is "Text/json", later opened FirebuG of the console, the original Content-type value is "Application/json". See figure Three.

3, the size of the response data is less than the configuration value of Compressionminsize.

Tomcat enables gzip compression to improve Web performance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.