HAProxy Enterprise Documentation 12.5

Redirecting HTTP Requests

HTTP compression is a technique that allows a server to compress the body of a response before it's forwarded to a client.

Its main purpose is to reduce the size of the response and limit the number of bytes to send out and enable faster delivery.

When a client sends an HTTP request, it announces the compression algorithm it supports using the Accept-Encoding header field. The server can compress its response using any algorithm supported by the client.

Since HAProxy is usually situated between clients and the servers, it can compress responses if a server does not do it for a client that supports it.

Using Traffic Compression

HAProxy can work in two modes:

  • Compression offloading: compresses all the streams on behalf of the servers

  • Compression catch up: compresses only the streams where the client supports compression but the server did not compress the response

HAProxy disables HTTP compression when:

  • The request does not advertise a supported compression algorithm in the Accept-Encoding header

  • The response message is not HTTP/1.1

  • HTTP status code is not 200

  • The response header Transfer-Encoding contains chunked (Temporary Workaround)

  • The response contains neither a Content-Length header nor a Transfer-Encoding whose last value is chunked

  • The response contains a Content-Type header whose first value starts with multipart

  • The response contains the no-transform value in the Cache-control header

  • User-Agent matches Mozilla/4 unless it is MSIE 6 with XP SP2, or MSIE 7 and later

  • The response contains a Content-Encoding header, indicating that the response is already compressed (see compression offload)


The compression does not rewrite Etag headers and does not emit the Warning header.

Configuring HAProxy Enterprise for HTTP Compression

SLZ is a fast and memory-less stream compressor which produces an output that can be decompressed with zlib or gzip.

HAProxy Enterprise is compiled with libslz, a compression algorithm which requires little memory to optimize performance.

The following directives from the global section apply to stateless compression:

maxcomprate <number>

sets the maximum per-process input compression rate to <number> kilobytes per second. For each new session, if the maximum value is already reached, no compression will occur. The default value is 0, which means no limit.

maxcompcpuusage <number>

sets the maximum per-process CPU usage (%) that HAProxy can reach before it stops compressing new responses. The default value is 100, which means no limitation.

tune.comp.maxlevel <number>

sets the maximum compression level. With libslz, only two values are valid: 0 which means no compression and 1 which means enable compression. The default value is 1.

The following directives from the defaults, frontend, listen or backend section apply to stateless compression:

compression algo <algorithm> ...

sets the list of supported compression algorithm. Many algorithm can serve as arguments: HAProxy applies the first one found on the client side Content-Encoding header field. The following algorithms are currently supported:


applies deflate compression with zlib format. Not recommended to be enabled with raw-deflate.


Not recommended for production, since client side implementation can differ.


applies gzip compression.


does not alter content at all; used for debugging purposes only.


applies deflate compression without zlib wrapper. May be used as an alternative to deflate.

compression type <mime type> ...

MIME type to which to apply compression. It is related to server side Content-Type HTTP header field. If not set, HAProxy tries to compress all responses not compressed by the server.

compression offload

In this mode, HAProxy removes the Accept-Encoding header field from the request before it forwards it to the server. Thus, HAProxy compresses the response and not the server.

Configuration Examples

Enable compression in front of application web servers which cannot compress content that was dynamically generated:

backend b_myapp
    # ...
    compression algo gzip

Enable compression in front of a web service for all text based content:

frontend f_myapp
    # ...
    compression algo gzip
    compression type text/css text/html text/javascript application/javascript text/plain text/xml application/json

Offload compression for a farm where a broken gzip or deflate implementation is running:

backend b_myapp
    # ...
    compression algo gzip
    compression offload

Next up

Rewriting HTTP Requests, Methods, or Headers