Web server configuration gzip compression improves site performance

Source: Internet
Author: User

Objective:

GZIP encoding on the HTTP protocol is a technique used to improve the performance of Web applications. High-traffic Web sites often use gzip compression technology to make users feel faster. This generally refers to the WWW server installed in a feature, when someone to access the server's website, the server of this feature will compress the content of the Web page to be displayed in the browser of the visiting computer. Generally, the plain text content can be compressed to 40% of the original size. So the transmission is fast, The effect is that when you click on the URL, it will show up quickly. This, of course, also increases the load on the server. This function module is installed in the general server.

First, 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.

For Tomcat this is:

<connector executor="Tomcatthreadpool"Port=" the"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 Server.xml in Tomcat:

    1. compression="on"Turn on compression
    2. compressionMinSize="2048"Files larger than 2KB are compressed
    3. noCompressionUserAgents="gozilla, traviata", for both browsers, do not compress
    4. compressableMimeType="text/html,text/xml,application/javascript,text/css,text/plain,text/json"Indicates support for compression of HTML, XML, JS, CSS, JSON and other file formats
Second, disable proxy caching using Cache-control
Import Java.io.ioexception;import Javax.servlet.filter;import javax.servlet.filterchain;import Javax.servlet.filterconfig;import Javax.servlet.servletexception;import Javax.servlet.servletrequest;import Javax.servlet.servletresponse;import Javax.servlet.http.HttpServletResponse; Public classCachecontrolfilter implements Filter { Public voidDoFilter (servletrequest request, servletresponse response, Filterchain chain) throws IOException, Servlete xception {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"); Resp.setheader ("Cache-control","max-age=0, Private");//Resp.setheader ("Pragma", "No-cache");Chain.dofilter (request, response); } @Override Public voiddestroy () {} @Override Public voidInit (Filterconfig arg0) throws servletexception {}}

Added 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>

Web server configuration gzip compression improves site 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.