Showing posts with label gzip. Show all posts
Showing posts with label gzip. Show all posts

Tuesday, May 4, 2010

Tomcat Performance: gzip Compression

One of the ways of increasing server performance, is by reducing the amount of data passed on the network. This can be done by using compression.
Most modern web browsers support compression. If a browser supports compression it will send in the request header the following string:
Accept-Encoding: gzip,deflate
The server can identify this header and return a compressed response. If the browser doesn’t send this header, the server will always return an uncompressed response. If the response is compressed, it will return in the response header the following string:
Content-Encoding: gzip
Now the client knows that the response is gzip compressed.
Tomcat supports gzip compression out of the box. You can easily add compression support to your server, without writing a single line of code.
To turn on Tomcat compression, you should edit server.xml, which is located under Tomcat conf directory. The compression is added to the connector element:
<Connector port="8080" protocol="HTTP/1.1" 
connectionTimeout="20000" redirectPort="8443" 
compression="on" compressableMimeType="text/html,
text/xml,text/plain,text/javascript,text/css" 
/>
Note that you can determine the mime types for which tomcat should do the compression.
Sometimes there are user agents (web browsers) that has problems with gzip compression, even If they send Content-Encoding: gzip direction in the request header. In order to exclude specific user agents from being served with gzip compression you can use the property: noCompressionUserAgents. You can use regular expression to define user agents list separated with commas:
<Connector port="8080" protocol="HTTP/1.1" 
connectionTimeout="20000" redirectPort="8443" 
compression="on" compressableMimeType="text/html,text/xml,text/plain,text/javascript,text/css" 
noCompressionUserAgents=".*MSIE 6.*"
/>


If a file is too small, the overhead of compressing it may take longer than sending it uncompressed. You can define a minimum size for which a compression should not be done:
<Connector port="8080" protocol="HTTP/1.1" 
connectionTimeout="20000" redirectPort="8443"
compression="on" compressableMimeType="text/html,text/xml,text/plain,text/javascript,text/css" 
noCompressionUserAgents=".*MSIE 6.*"
compressionMinSize="1024"
/>