Configure Tomcat and Apache gzip compression (HTTP compression) to speed up website browsing

Source: Internet
Author: User

When the data requested by a website from the server is large, loading of the browser is slow under limited bandwidth. Sometimes, the page does not respond and the user experience is poor, tomcat provides us with an effective solution, that is, using compression to solve the transmission problem.
Tomcat uses HTTP/1.1 gzip for compression to reduce bandwidth pressure,
First, we will introduce gzip:
Gzip encoding on HTTP is a technology used to improve the performance of Web applications. Large-Traffic web sites often use gzip compression technology to make users feel faster. This generally refers to a function installed on the WWW server. When someone visits the website on this server, this function in the server compresses the webpage content and then transmits it to the visiting Computer Browser for display. generally, the plain text content can be compressed to 40% of the original size. in this way, the transmission is faster, and the result is that you click the URL and it will be displayed soon. of course, this will also increase the server load. this function module is installed on servers.
We use a simple example to describe how to use Tomcat compression: (use firebug to view request information)

The first is a simple servlet:

Content:

public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        response.setContentType("text/html");        PrintWriter out = response.getWriter();        out                .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");        out.println("<HTML>");        out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");        out.println("  <BODY>");        out.print(" <select> ");        for (int i = 0; i < 100000; i++) {            out.print("<option>testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttest"+i+"</option>");        }        out.println("</select>");        out.println("  </BODY>");        out.println("</HTML>");        out.flush();        out.close();    }

We will output a drop-down box with 100,000 options. When compression is not used:


Request data reaches 9 MB
, Probably calculate my download time (4 m Great Wall broadband): 9*1024/(4*1024/8) = 18 m, plus multi-person shared bandwidth, that is, about 20 m. Firebug shows 20.96.

The website experience at this speed must be poor. Next we will use Tomcat compression to look at it:

Of course, you need to configure it:
You can use the following parameters:

Compression = "on"
Whether to enable compression on is enabled (text data compression) off is disabled, force compresses all data
Compressionminsize1 = "2048"
Compression is performed only when the minimum data size is exceeded.
Nocompressionuseragents = "gozilla, Traviata"
Which clients do not compress the requests, but are not limited by default
Compressablemimetype = "text/html, text/XML, text/JavaScript, text/CSS, text/plain"
Configure the data type to be compressed. The default value istext/html,text/xml,text/plain

The configuration is as follows:

<Connector port="8088" protocol="HTTP/1.1"                connectionTimeout="20000"                redirectPort="8443"               compression="on"                compressionMinSize1="2048"                noCompressionUserAgents="gozilla, traviata"                compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain"/>

After starting, check again:


The data is compressed to 274.7 kb, and the response speed is reduced by half. Of course, it takes time to compress server data and decompress browser data.


The example is complete,
Of course, the compression technology of Tomcat has many other features. However, for requests with large data volumes, not only need to use this technology, but also need to work hard elsewhere, such as Ajax technology and cache,
I hope you will learn more. Thank you.

Reprinted from: http://www.blogjava.net/freeman1984/archive/2010/09/15/332121.html

Apache configuration gzip please see: http://www.jzxue.com/fuwuqi/http-iis-apache/200901/13-1111.html

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.