Thursday, December 14, 2006

mod_gzip for Apache

Apache's mod_gzip provides good compression for server output. I just got it working on my server with little bit of tweaking done in the configuration. I was using the debian server.

Install mod_zip if you don't have it already:

apt-get install libapache-mod-gzip

Load the module mod_gzip: (add this line in httpd.conf or modules.conf)
LoadModule gzip_module /usr/lib/apache/1.3/mod_gzip.so
Look at the example http://schroepl.net/projekte/mod_gzip/config.htm configuration file.

Here is the configurtaion I added:
<IfModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_can_negotiate Yes
mod_gzip_static_suffix .gz
AddEncoding gzip .gz
mod_gzip_command_version '/mod_gzip_status'
mod_gzip_keep_workfiles No
mod_gzip_minimum_file_size 500
mod_gzip_maximum_file_size 500000
mod_gzip_maximum_inmem_size 60000
mod_gzip_handle_methods GET POST

mod_gzip_item_exclude reqheader "User-agent: Mozilla/4.0[678]"

mod_gzip_item_include file \.html$

mod_gzip_item_include file \.js$
mod_gzip_item_include file \.css$

mod_gzip_item_exclude mime ^image/
mod_gzip_item_include mime ^text/

# This was very important to get things working
mod_gzip_item_include uri .*

mod_gzip_dechunk Yes

LogFormat "%h %l %u %t \"%V %r\" %<s %b
mod_gzip: %{mod_gzip_result}n In:%{mod_gzip_input_size}n -<
Out:%{mod_gzip_output_size}n = %{mod_gzip_compression_ratio}n
pct." common_with_mod_gzip_info2

CustomLog /var/log/apache/mod_gzip.log common_with_mod_gzip_info2
</IfModule>
I had to add the configuration line mod_gzip_item_include uri .* until then I saw only mod_gzip: DECLINED:EXCLUDED In:0 -< Out:0 = 0 pct. in the mod_zip.log.
Look at http://www.debian-administration.org/articles/31 which reports problem with mod_gzip and mod_ssl, so added this line of configuration as well. Add this only if you enabled ssl access for your server.

<VirtualHost *:443>
<IfModule mod_ssl.c>
...
</IfModule>

<IfModule mod_gzip.c>
mod_gzip_on No
</IfModule>
</VirtualHost>

NOTE: With Apache2 you can use mod_deflate for the gzip compression. To get more details look at
http://httpd.apache.org/docs/2.0/mod/mod_deflate.html

Locale Dependent DateFormat

Recently I came across a strange problem on the debian server install.

The following piece of jython code which was working fine on my machine failed to work on debian server.

import java.text.DateFormat as DateFormat
format = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
format.setLenient(1);
print format.parse('Nov 6, 2006 3:23:54 PM');

I was getting this exception:
Traceback (innermost last):
File "<string>", line 5, in ?
java.text.ParseException: Unparseable date: "Nov 6, 2006 3:23:54 PM"
at java.text.DateFormat.parse(DateFormat.java:335)
...

I looked at the default Locale that was used and it was: [command used: locale]
LANG=en_IN
LC_CTYPE="en_IN"
LC_NUMERIC="en_IN"
LC_TIME="en_IN"
LC_COLLATE="en_IN"
LC_MONETARY="en_IN"
LC_MESSAGES="en_IN"
LC_PAPER="en_IN"
LC_NAME="en_IN"
LC_ADDRESS="en_IN"
LC_TELEPHONE="en_IN"
LC_MEASUREMENT="en_IN"
LC_IDENTIFICATION="en_IN"
LC_ALL=

I did the following and got the piece of code working:
export LC_CTYPE="en_US.UTF-8"
[If this does not solve try export LC_TIME="en_US.UTF-8"]

After this I could got the following output:
Mon Nov 06 15:23:54 IST 2006

Make sure you set the right LOCALE before using any API that depends on SYSTEM LOCALE information. Few Java API's allows you to specify the Locale as an argument use it when required.

More details about Java 1.3 Locale

I found a related post Don't forget to specify the locale

This page is powered by Blogger. Isn't yours?