Deflatemod is a module for Ocsigen that compresses content using the HTTP Content-Encoding mechanism. It supports gzip, deflate (zlib format) and zstd.
Brotli (br) is not supported yet: at the time of writing there is no maintained OCaml binding to libbrotli that builds on OCaml 5.
You need the bytesrw library (with its zlib and zstd backends, which require the system zlib and libzstd libraries) and a working Ocsigen installation.
To use that extension, load OCamlfind package ocsigenserver.ext.deflatemod, either from your Dune file (if you are using static linking), or from the configuration file:
<extension findlib-package="ocsigenserver.ext.deflatemod" />
This page describes the configuration file options. If you are building a statically linked executable without configuration file, use the corresponding functions from module Deflatemod.
Again, the options are in ocsigen.conf. You can set a few options globally, in the extension tag:
gzip and deflate; zstd uses the library default level.For example:
<extension module="/home/gabriel/ocsigen/extensions/deflatemod.cmo"> <compress level="6" /> <buffer size="1024" /> </extension>
Next, you '''have to''' specify which pages you whish to deflate, in which host/site (otherwise, deflatemod won't compress anything). See next question to do that.
To some extent, yes. You can '''exclude''' some files and deflate every other, according to their mime-type or extension (''ie.'' the end of the '''url''' in fact, not really the file name on the disk).
Here is an example:
<host>
<site>
<!-- ... -->
</site>
<deflate compress="allbut">
<!-- deflate everything but *.tar.gz files and image/* files -->
<extension>.tar.gz</extension>
<type>image/*</type>
</deflate>
</host>As you can see, you must put the <deflate> tag at the very end of the <host> element. Indeed, Ocsigen deals with extensions sequentially, so if you put <site> or other elements '''after''' <deflatemod>, they won't be compressed.
Conversely, you can choose to deflate '''only''' some files, based on their mime-types or extension:
<host>
<site>
<!-- ... -->
</site>
<deflate compress="only">
<!-- deflate only text/html files -->
<type>text/html</type>
</deflate>
</host>Warning: you can use only one contenttype tag! So you must choose, either the "only" or the "allbut" way.
Limitations: you cannot filter by directory or by filename extension (remember extension is the url's extension). If you really need it, please fill a bug report or send a patch.
You can't. Deflatemod follows the RFC and chooses automatically the right encoding, based upon the Accept-Encoding header sent by the client. When the client offers several codecs at the same quality, zstd is preferred, then gzip, then deflate.
Yes. When you run ocsigenserver ./public (or ocsigenserver --serve ...), responses with a text-based content type are compressed by default, with no configuration needed, just like a static file server such as Caddy.
No, it doesn't. Content-encoding is definitely a better solution, providing end to end compression at the content-level.
As it cannot compute the new content-length, deflatemod uses chunked-encoding. This is the recommended way to go.
Page written by Gabriel Kerneis