New features realeased in NaviServer 4.99.6 makes it simpler to serve gzip encoded responses to clients.

NaviServer can now return gzip encoded static files and dynamic reponses if the client's request specifies the http header Accept-Encoding: gzip.

Compress Responses for Dynamic Content

The following NaviServer configuration parameters control whether ADP and ns_return response data can be served compressed, by default, if the client requests a compressed response.

ns_section "ns/server/${servername}"
...
...
# Compress response character data: ns_return, ADP etc.
ns_param  compressenable   true    ;# Boolean. Default; use ns_conn compress to override
ns_param  compresslevel    4       ;# 1-9 where 9 is high compression, high overhead
ns_param  compressminsize  512     ;# Compress responses larger than this

Compress Responses for Static Files Served by Fastpath

The following NaviServer configuration parameters control whether fastpath can serve gzip encoded static files, if the client requests a compressed response.

ns_section "ns/fastpath"
...
...
# Compress response for static files served by fastpath
ns_param  gzip_static  true             ;# Boolean. Return gzip-ed variant, if available and allowed by client, 
                                        ;# default is false.
 
                                        ;# When a file path/foo.ext is requested, and there exists a file
                                        ;# path/foo.ext.gz, and the timestamp of the gzip-ed file is equal or 
                                        ;# newer than the source file, use the gzip-ed file for delivery.
ns_param  gzip_refresh  true            ;# Boolean. Refresh stale .gz files on the fly using ::ns_gzipfile, 
                                        ;# default is false.
ns_param  gzip_cmd  "/usr/bin/gzip -9"  ;# Command for gzip-ing files, used by ::ns_gzipfile.

If you want to be able to return gzip encoded responses for static files you must ensure that .gz versions of the un-compressed static files are available on the filesytem. The following filter can be used to dynamically seed the filesystem with .gz versions of static files.

proc filter_fastpath_gzip {filter_when file_extensions} {
    #| Postauth filter to seed filesystem with .gz versions of static files if the client accepts gzip-ed content.
    set conn_path [qc::conn_path]
    set file_path [ns_pagepath]$conn_path
    set gzipped_file_path [ns_pagepath]${conn_path}.gz
    
    if { [ns_conn zipaccepted] \
             && ![file readable $gzipped_file_path] \
             && [file readable $file_path] \
             && ( [llength $file_extensions] == 0 || [file extension $file_path] in $file_extensions ) \
             && [file type $file_path] in [list "link" "file"] \
         } {
        ns_gzipfile $file_path $gzipped_file_path
    }
    return "filter_ok"
}
# gzip delivery of static .js and .css files
ns_register_filter postauth $http_method /* filter_fastpath_gzip [list .js .css]

Once you have initially seeded the filesystem with .gz versions of static files NaviServer will automitically re-compress the .gz versions if gzip_refresh is set to true and the mtime of the un-compressed static file is newer than the compressed .gz file.