Configure gzip compression for Tomcat and Apache

Source: Internet
Author: User

 

HTTP compression can greatly improve the speed of website browsing. The principle is that after the client requests a webpage, the server compresses the webpage file and downloads it to the client, the client browser is responsible for decompression and browsing. Compared with ordinary browsing processes such as HTML, CSS, JavaScript, and text, it can save about 40% of the traffic. More importantly, it can compress the pages dynamically generated, including CGI, PHP, JSP, ASP, Servlet, shtml, and other output pages. The compression efficiency is amazing.

1. For Versions later than tomcat, the output content can be compressed using the gzip compression format.

The following is the original content of $ tomcat_home $/CONF/server. XML in tomcat5.5.20.

1 <connector Port = "80" maxhttpheadersize = "8192"
2 maxthreads = "150" minsparethreads = "25" maxsparethreads = "75"
3 enablelookups = "false" redirectport = "8443" acceptcount = "100"
4 connectiontimeout = "20000" disableuploadtimeout = "true" uriencoding = "UTF-8"/>
5 <! -- Note: To disable connection timeouts, set connectiontimeout Value
6 to 0 -->
7
8 <! -- Note: To use gzip compression you cocould set the following properties:
9
10 compression = "on"
11 compressionminsize = "2048"
12 nocompressionuseragents = "gozilla, Traviata"
13 compressablemimetype = "text/html, text/XML"
14 -->

From above8th rowsYou can add the following attributes to the connector instance to use the gzip compression function.
1) compression = "on" enable the compression function
2) compressionminsize = "2048" enables the size of compressed output content. The default value is 2 kb.
3) nocompressionuseragents = "gozilla, Traviata" for the following browsers, compression is not enabled
4) compressablemimetype = "text/html, text/XML" compression type

The configuration content here is:

1 <connector Port = "80" maxhttpheadersize = "8192"
2 maxthreads = "150" minsparethreads = "25" maxsparethreads = "75"
3 enablelookups = "false" redirectport = "8443" acceptcount = "100"
4 connectiontimeout = "20000" disableuploadtimeout = "true" uriencoding = "UTF-8"
5 compression = "on"
6 compressionminsize = "2048"
7 nocompressionuseragents = "gozilla, Traviata"
8 compressablemimetype = "text/html, text/XML, text/JavaScript, text/CSS, text/plain"/>
9 <! -- Note: To disable connection timeouts, set connectiontimeout Value
10 to 0 -->
11
12 <! -- Note: To use gzip compression you cocould set the following properties:
13
14 compression = "on"
15 compressionminsize = "2048"
16 nocompressionuseragents = "gozilla, Traviata"
17 compressablemimetype = "text/html, text/XML"
18 -->
19

PS: for some text files such as log and txt, we can also let the server adopt gzip compression for transmission, modify web. xml under the conf directory, and add

<Mime-mapping>
<Extension> log </extension>
<Mime-type> text/plain </mime-type>
</Mime-mapping>

You can specify the compression and transmission. Generally, compression transmission can greatly increase the display speed.

 

2. There are two scenarios for Apache:
1) For versions earlier than apache2.0, it was originally not supported, but can be enabled by adding a third-party module_gzip Module
2) Apache supports apache2.0 and later versions, but not gzip, but mod_deflate
The following describes apache2.0 and later versions.
1) Remove # comments before loadmodule headers_module modules/mod_headers.so #,
2) Add loadmodule deflate_module modules/mod_deflate.so
3) add in virtualhost

1 <location "/">
2 setoutputfilter deflate
3 browsermatch ^ Mozilla/4 gzip-only-text/html
4 browsermatch ^ Mozilla/4/. 0 [678] No-Gzip
5 browsermatch/bmsie! No-gzip! Gzip-only-text/html
6 setenvifnocase request_uri /.(? : GIF | jpe? G | PNG) $ no-gzip dont-vary
7 header append vary User-Agent Env =! Dont-vary
8 </location>
9

I have a complete demonstration here

1 # load the deflate Module
2 loadmodule headers_module modules/mod_headers.so
3 loadmodule deflate_module modules/mod_deflate.so
4 <virtualhost *: 80>
5 DocumentRoot F:/apachetest
6 <location "/">
7 setoutputfilter deflate
8 browsermatch ^ Mozilla/4 gzip-only-text/html
9 browsermatch ^ Mozilla/4/. 0 [678] No-Gzip
10 browsermatch/bmsie! No-gzip! Gzip-only-text/html
11 setenvifnocase request_uri /.(? : GIF | jpe? G | PNG) $ no-gzip dont-vary
12 header append vary User-Agent Env =! Dont-vary
13 </location>
14 </virtualhost>

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.