Forum OpenACS Q&A: Re: How to enable gzip compression

Posted by Gustaf Neumann on
For NaviServer, the situation is simpler. NaviServer does not require an extra module, the "configure" script determines at compile time of NaviServer the availability of zlib (which is pretty ubiquitous). In the startup script, one can enable/configure gzip compression for dynamic content (adp pages) via the following parameters:
ns_section ns/server/${server} 
   # Compress response character data: ns_return, ADP etc.
   ns_param    compressenable	on	;# false, use "ns_conn compress" to override
   #ns_param    compresslevel       4	;# 4, 1-9 where 9 is high compression, high overhead
   #ns_param    compressminsize     512	;# Compress responses larger than this
So far Naviserver does not handle gzip compression for static content automatically. Does one really want to gzip a 500MB video file? For static content one would need at least some additional configuration on what files compression is wanted. It can be discussed, whether it is desirable to compress static content dynamically.

With the following snippet, one can deliver "precompressed" static files, if the client accepts gzipped content. To generate the gzipped content, one can iterate with a script over the file system, and produce the compressed content in addition to the uncompressed content.

set file graph.js
set fn [ns_info pageroot]/$file
set mime [ns_guesstype $file]

if {[ns_conn zipaccepted] && [ns_set iget [ns_conn headers] Range] eq ""} {
    if {![file readable $fn.gz] || [file mtime $fn] > [file mtime $fn.gz]} {
    exec gzip -9 <  $fn > $fn.gz
    if {[file readable $fn.gz]} {
    set fn $fn.gz
    ns_set put [ns_conn outputheaders] Vary Accept-Encoding
    ns_set put [ns_conn outputheaders] Content-Encoding gzip
ns_returnfile 200 $mime $fn 
The only NaviServer specific part is ns_conn zipaccepted which evaluates the "Accept-Encoding" rules from the client. This option was added recently and requires the head version, which is currently in quite active development (several commits every week). To get a more conservative version, download the tarfile for tag "naviserver-4.99.4" from Nov. 2012. It contains as well a sample startup script for openacs and works well with recent versions of OpenACS.

Hope this helps

Gustaf Neumann