Most modern web browsers support compression. If a browser supports compression it will send in the request header the following string:
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:Accept-Encoding: gzip,deflate
Now the client knows that the response is gzip compressed.Content-Encoding: gzip
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:
Note that you can determine the mime types for which tomcat should do the compression.<Connector port="8080" protocol="HTTP/1.1"connectionTimeout="20000" redirectPort="8443"compression="on" compressableMimeType="text/html,text/xml,text/plain,text/javascript,text/css"
/>
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"
/>
Good think to have gzip compression in Tomcat.
ReplyDeleteThere are also several other ways to improve tomcat's performance. I wrote about them here: http://touk.pl/blog/2010/08/23/glimpse-on-tomcat-performance-tuning/ just look if You are interested.
Greetings
This post is specifically for Tomcat compression.
ReplyDeleteI read you post. It is a nice addition. Thanks.
the book Pro Apache Tomcat 6 says that the value of the compression can be yes, no, or the minimum size, and they do not list a compressionMinSize attribute to the Connector. Is this new for Tomcat 6?
ReplyDeleteSorry, I have no idea how it used to be on versions before Tomcat6. On Tomcat6 this parameter should be the one.
ReplyDeleteHi Guy,
ReplyDeletethanks for the post. Unfortunately I think there is an error. I just copy-pasted your code and got angry because it was working for html but not for css and javascript. I found out there seems to be a spelling mistake in your connector definition: the attribute is called compressableMimeType not compressableMimeTypes. There is no "s" on the end. Maybe you want to correct this.
Cheers
Marc
Hi Guy,
ReplyDeletethanks for the blog post, but I think there is a spelling mistake. The attribute for the connector is called "compressableMimeType" not "compressableMimeTypes" (no "s" at the end). I copy-pasted your connector definition, and it just gzipped the html, but not css and javascript. This is the default. With the correct attribute it gzips everything properly.
Cheers
Marc
Hi Marc, thanks for the correction. I will fix the blog post.
ReplyDeleteHi, i tried this for PDF files in windows platform. It didn't work. Is this for only text response?
ReplyDeleteThanks.