Configure Tomcat HTTP connector Apache to increase Server Performance

Compression is a simple, effective way to save bandwidth and speed up your site. If handled carefully, it will reduce the page download time increasing with user views and visits.  Before I tell you about the performance and other advantages, let us know how it come in picture.

Whenever a user request a page with the help of his/her browser. His/Her starts communicating with web server where the application is hosted, whom he/she had requested. This conversation goes like this:

  • Browser: GET File
  • Server: Checks if the file exists or not in web context.
  • Server: If found,  Response code  is 200(Ok) and sending the file otherwise Response code 404.
  • Browser: 1000KB? oh sending the compelete page, it is downloaed at user system,then user is able to view the response from the server...




Now if we enable gzip we could send a .zip file to the browser (filename.zip) instead of old html file.In this way we save on bandwidth and download time.  Now the browser-server conversation might look like this:


All the step are same as in earlier case only change is before sending the response, webserver will compress it and then browser is going to unzip it before display.

Enable Compression in Apache 

mod_deflate module in apache provides the DEFLATE output filter. This module compress the output from your server  before being sent to the client over the network.

How to Enable Compression
Compression is implemented by the DEFLATE filter. To use it, enable DEFLATE option by using following lines in configuration file.

SetOutputFilter DEFLATE

Given below is a sample configuration:
<IfModule mod_deflate.c>
          # these are known to be safe with MSIE 6
          AddOutputFilterByType DEFLATE text/html text/plain text/xml

          # everything else may cause problems with MSIE 6
          AddOutputFilterByType DEFLATE text/css
          AddOutputFilterByType DEFLATE application/x-javascript application/javascript application/ecmascript
          AddOutputFilterByType DEFLATE application/rss+xml
</IfModule>


Input Decompression
The mod_deflate module also work as filter for decompressing a gzip compressed request.To activate it you have to insert the DEFLATE filter into the input filter chain using SetInputFilter or AddInputFilter. After applying this filter a request having a content-encoding: gzip header, will be automatically decompressed.

<Location /web-folder>
    SetInputFilter DEFLATE
</Location>


Enable GZIP Compression in Tomcat server

To enable compression in Tomcat we have to edit the server.xml file located in TOMCAT_HOME or CATALINA_HOME directory(linux) and at CATALINA_HOME/conf directory(windows). After opening file in a editor look for the Connector tag. it looks like this :

 <Connector port="8080" protocol="HTTP/1.1"  connectionTimeout="20000"
               redirectPort="8443" />

After updating configuration of compression, Connector tag will be like the one given below:

   <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               compression="on" compressionMinSize="2048" noCompressionUserAgents="gozilla, traviata"               compressableMimeType="text/html,text/xml,text/plain,text/javascript,text/css,image/jpg,image/jpeg,image/gif,image/png"
               URIEncoding="UTF-8"
               redirectPort="8443" />


compression : Flag use to enable compression in Tomcat. You can set the value of flag to "on" (allow compression, which causes text data to be compressed), "off" (disable compression) , "force" (forces compression in all cases) or a numerical integer value (which is equivalent to "on", but specifies the minimum amount of data before the output is compressed). If the content-length is not known and compression is set to "on" or more aggressive, the output will also be compressed. If it is missing in tag then it's value will be  "off".

compressableMimeType : Value of this parameter is a comma separated list of MIME types for which HTTP compression may be used. You can add javascript and css also in list to be zipped along with html. The default value is text/html,text/xml,text/plain.


noCompressionUserAgents
It a Regular expression that defines the user agents to not use gzip.


Improvement in Data transfer and Page loading interval
Once you have done the setting, restart the tomcat server and then look at the size of the webpage and the time taken for loading. You will observe page loading time is decreased approximately by 70% to 90% as well as the size of data transferred between network is also reduced by same ratio. You can also check the if the page is compressed or not at http://www.seoconsultants.com/tools/compression

No comments:

Post a Comment