Network performance

Compression

HTTP compression allows you to shrink the body of a response before it’s relayed to a client which results in using less network bandwidth per request. From a client’s perspective, this reduces latency.

Enable compression for responses Jump to heading

  1. In a frontend, backend, listen or defaults section, add the filter compression and compression algo directives. Set compression algo to one of the following:

    • deflate
    • gzip
    • identity
    • raw-deflate
    haproxy
    backend web_servers
    filter compression
    compression algo gzip
    haproxy
    backend web_servers
    filter compression
    compression algo gzip

    Responses will be compressed only if they are not already compressed.

  2. Add a compression type directive, which specifies the MIME types to compress:

    haproxy
    backend web_servers
    filter compression
    compression algo gzip
    compression type text/css text/html text/javascript application/javascript text/plain text/xml application/json
    haproxy
    backend web_servers
    filter compression
    compression algo gzip
    compression type text/css text/html text/javascript application/javascript text/plain text/xml application/json
  3. Optional: Add a compression offload directive, which states that the load balancer will remove the Accept-Encoding header before forwarding a request to backend servers, which prevents the servers from performing compression, offloading that work to the load balancer. The compression offload directive may only be placed in frontend, listen, and backend sections:

    haproxy
    backend web_servers
    filter compression
    compression algo gzip
    compression type text/css text/html text/javascript application/javascript text/plain text/xml application/json
    compression offload
    haproxy
    backend web_servers
    filter compression
    compression algo gzip
    compression type text/css text/html text/javascript application/javascript text/plain text/xml application/json
    compression offload

Enable compression for requests Jump to heading

Available since

  • HAProxy 2.8
  • HAProxy Enterprise 2.8r1
  • HAProxy ALOHA 15.5

You can also shrink the body of a request before it’s relayed to a backend server.

  1. In a frontend, backend, listen or defaults section, add the filter compression and compression direction directives. Set the compression direction to one of:

    • request to compress only requests
    • response to compress only responses
    • both to compress both requests and responses
    haproxy
    backend web_servers
    filter compression
    compression direction both
    haproxy
    backend web_servers
    filter compression
    compression direction both
  2. Specify the compression algorithm:

    • If you set compression direction to request or both, add an algo-req directive.
    • If you set compression direction to response or both, add an algo-res directive.

    Set the compression algorithm to one of the following:

    • deflate
    • gzip
    • identity
    • raw-deflate.

    You can choose different compression algorithms for responses and requests.

    haproxy
    backend web_servers
    filter compression
    compression direction both
    compression algo-req deflate
    compression algo-res gzip
    haproxy
    backend web_servers
    filter compression
    compression direction both
    compression algo-req deflate
    compression algo-res gzip

    Responses and requests will be compressed only if they are not already compressed:

  3. Specify the MIME types to compress for requests and / or responses depending on which compression direction you specified:

    • If you set compression direction to request or both, add a compression type-req directive.
    • If you set the compression direction to response or both, add a compression type-res directive.

    You can specify different MIME types for requests and responses.

    haproxy
    backend web_servers
    filter compression
    compression direction both
    compression algo-req deflate
    compression type-req application/json text/xml text/plain
    compression algo-res gzip
    compression type-res text/css text/html text/plain text/xml text/javascript application/javascript application/json
    haproxy
    backend web_servers
    filter compression
    compression direction both
    compression algo-req deflate
    compression type-req application/json text/xml text/plain
    compression algo-res gzip
    compression type-res text/css text/html text/plain text/xml text/javascript application/javascript application/json
  4. Optional: Add a compression offload directive, which states that the load balancer will remove the Accept-Encoding header before forwarding a request to backend servers, which prevents the servers from performing compression, offloading that work to the load balancer. The compression offload directive may only be placed in frontend, listen, and backend sections:

    haproxy
    backend web_servers
    filter compression
    compression direction both
    compression algo-req deflate
    compression type-req application/json text/xml text/plain
    compression algo-res gzip
    compression type-res text/css text/html text/plain text/xml text/javascript application/javascript application/json
    compression offload
    haproxy
    backend web_servers
    filter compression
    compression direction both
    compression algo-req deflate
    compression type-req application/json text/xml text/plain
    compression algo-res gzip
    compression type-res text/css text/html text/plain text/xml text/javascript application/javascript application/json
    compression offload

Process-level settings Jump to heading

Add any of the following directives to the global section to customize compression:

Directive Description
maxcomprate int Sets the maximum per-process input compression rate to a number of kilobytes per second. For each session, if the maximum is reached, the compression level will be decreased during the session. If the maximum is reached at the beginning of a session, the session will not compress at all. If the maximum is not reached, the compression level will be increased up to tune.comp.maxlevel. A value of zero means there is no limit, this is the default value.
maxcompcpuusage int Sets the maximum CPU usage the load balancer can reach before stopping the compression for new requests or decreasing the compression level of current requests. It works like maxcomprate but measures CPU usage instead of incoming data bandwidth. The value is expressed in percent of the CPU used by the load balancer. A value of 100 disables the limit. The default value is 100. Setting a lower value will prevent the compression work from slowing the whole process down and from introducing high latencies.
tune.comp.maxlevel int Sets the maximum compression level. The compression level affects CPU usage during compression. Each session using compression initializes the compression algorithm with this value. The default is 1.

See also Jump to heading

Do you have any suggestions on how we can improve the content of this page?