Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Methods for GZIP to compress Tomcat and improve the performance of web

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the method of GZIP compression Tomcat and improve the performance of web, the content is clear, interested friends can learn, I believe it will be helpful after reading.

I. Preface

Recently, I worked on a project and encountered such a problem: the amount of json data returned by the server to the client is too large (about 65 MB), and it takes more than 1 minute to load on the client (of course, the loading time is also related to the local downlink bandwidth), which is time-consuming and traffic-consuming, and the user experience is extremely poor. Later, the method of online search and optimization is Http compression.

HTTP compression can greatly improve the speed of browsing the website. its principle is that after the client requests the corresponding resources from the server, the resource files are compressed from the server and then output to the client, and the browser of the client is responsible for decompressing and browsing. That is, reduce the response time by reducing the size of the HTTP response. Compared with the normal browsing process HTML, CSS,Javascript, Text, it can save about 40% of the traffic. More importantly, it can compress dynamically generated web pages, including CGI, PHP, JSP, ASP, Servlet,SHTML and so on, with high compression efficiency. GZIP itself is a network flow compression algorithm, and it is widely used. This article is intended to configure GZIP compression for apache tomcat 8.0.47. The browser uses Mozilla Firefox 35.0.1, debugging uses its own Firebug, and the following screenshots related to the network are from the Firebug console.

II. Brief introduction of Gzip compression

1. HTTP protocol supports GZIP compression mechanism, also known as protocol compression. HTTP GZIP compression is a protocol followed by both WEB servers and browsers, which means that both WEB servers and browsers must follow. At present, mainstream servers and browsers support GZIP compression technology. Including Chrome, IE, FireFox, Opera, etc.; servers include tomcat, Apache, IIS and so on.

2. GZIP is mainly used to compress static text files such as html,css,javascript, and also supports compression of dynamically generated web pages, including CGI, PHP, JSP, ASP, Servlet,SHTML and other output.

3. The ratio of GZIP compression is usually between 3 and 10 times, which can greatly save the network bandwidth of the server and greatly improve the browsing speed of the browser.

4. GZIP is a data compression format, which defaults and currently only uses deflate algorithm to compress data; deflate is a compression algorithm and an enhancement of huffman coding.

5. Protocol compression is based on the HTTP protocol, which does not require programmers to compress and decompress the code, but gives the compression process to the WEB server and the decompression process to the client. If the client is a browser that supports GZIP compression, then the decompression process does not require the participation of programmers, and the browser will decompress automatically according to certain rules; if the client is HttpClient, then manual GZIP decoding is required.

6. Compression process: the client sends a http request. If the request header header contains Accept-Encoding:gzip,deflate (current browsers generally default to this), then the browser means that the server needs to perform GZIP compression, and then see whether the type of response content meets the type configured by the server. If so, the WEB server will compress the response content before transmitting it. And add Content-Encoding gzip to the response header If it does not match, it will not be compressed and will be returned directly.

7. Decompression process: (browser) the client receives the response, and if the response header contains Content-Encoding GZIP, the browser automatically decompresses the response with GZIP before rendering it on the page. If not, it will be rendered directly on the page.

The shortcomings of 8.GZIP. Compared with the projects without GZIP, the use of GZIP increases the pressure of server compression (cpu consumption) and client decompression, so the configuration of the server is more demanding. In addition, compression also takes time. If you want to take up less space and get a high compression ratio, you must sacrifice a long time. On the other hand, if time is more precious and requires speed, then the resulting compression ratio must be smaller, which will of course take up more space (compression ratio = original content size / post-compression size, the greater the compression ratio. It shows that the smaller the compression package that takes up space after compression), which is the contradiction between physical space and time.

3. Configuration method in tomcat

Version requirements: Tomcat5.0 or above. Modify% TOMCAT_HOME%/conf/server.xml, and the revision node is as follows:

Parameter description:

1. Compression= "on" enables compression. Optional values: "on" on, "off" off, "force" on in any case.

2. Only files with compressionMinSize= "2048" larger than 2KB are compressed. Used to specify the minimum data size for compression, in B, with a default of 2048B. Pay attention to the size of this value, if the configuration is unreasonable, the consequence is that the small files become larger after compression, which can not achieve the desired effect.

3. NoCompressionUserAgents= "gozilla, traviata". For these two browsers, there is no compression (I don't know what these two browsers are, but I can't find them on Baidu). Their values are regular expressions, and the matching UA will not be compressed by default.

4, compressableMimeType= "text/html,text/xml,application/javascript,text/css,text/plain,text/json" will be compressed list of MIME types, multiple commas separated, indicating support for html, xml, js, css, json and other file formats of compression (plain is unformatted, but for what it is, I compare vague concepts). CompressableMimeType is important because it is used to tell tomcat what kind of file to compress. If the type is specified incorrectly, it must not be compressed. So, how do you know the type of file to compress? It can be found in the following way.

4. Check whether the configuration is successful

Restart tomcat after modification, and finally check the website: query the effect of http://seo.chinaz.com/?host=iitshare.com.

5. Common errors (no effect after configuration)

You can troubleshoot through the following steps:

1. The configuration parameters in tomcat are misplaced. Note that the configuration parameters should be written to area An instead of area B, which is the Connector of protocol= "HTTP/1.1" in the following figure.

2. The response data is not the type of compressableMimeType parameter configuration. I encountered this pit, and the front and back end of our project is transmitted using json. So at first I thought it was "text/json", then I opened the console of Firebug and it turned out that the value of Content-Type was "application/json". See figure 3.

3. The size of the response data is less than the configuration value of compressionMinSize.

Attached: optimization result

You can see that the compression ratio = 65.68.4 = 7.810 and the time ratio = 96 / 16.2 = 5.926, which is already ideal.

After reading the above content, do you have a further understanding of how GZIP can compress Tomcat and improve the performance of web? if you want to learn more, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report