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. This reduces latency from a client’s perspective.
Enable compression for responses Jump to heading
-
In a
frontend,backend,listenordefaultssection, add the properfilterdirective depending on the load balancer version:- HAProxy 3.4 and newer: Use
filter comp-res. The examples in this section use this syntax. - HAProxy Enterprise all versions/HAProxy 3.3 and older: Use
filter compression. This syntax is deprecated and will become unsupported in a future release.
- HAProxy 3.4 and newer: Use
-
Add the
compression algodirective and set it to one of the following:deflategzipidentityraw-deflate
Example for HAProxy 3.4 and newer:
haproxybackend web_serversfilter comp-rescompression algo gziphaproxybackend web_serversfilter comp-rescompression algo gzipResponses will be compressed only if they aren’t already compressed.
-
Add a
compression typedirective, which specifies the media types to compress:haproxybackend web_serversfilter comp-rescompression algo gzipcompression type text/css text/html text/javascript application/javascript text/plain text/xml application/jsonhaproxybackend web_serversfilter comp-rescompression algo gzipcompression type text/css text/html text/javascript application/javascript text/plain text/xml application/json -
Optional: Add a
compression offloaddirective, which states that the load balancer will remove theAccept-Encodingheader before forwarding a request to backend servers. This prevents the servers from performing compression, offloading that work to the load balancer. Thecompression offloaddirective may only be placed infrontend,listen, andbackendsections:haproxybackend web_serversfilter comp-rescompression algo gzipcompression type text/css text/html text/javascript application/javascript text/plain text/xml application/jsoncompression offloadhaproxybackend web_serversfilter comp-rescompression algo gzipcompression type text/css text/html text/javascript application/javascript text/plain text/xml application/jsoncompression offload
Enable compression for requests Jump to heading
This section applies to:
- HAProxy 2.8 and newer
- HAProxy Enterprise 2.8r1 and newer
- HAProxy ALOHA 15.5 and newer
Your web server must support request body compression
Does your web server support decompressing requests? Most don’t. This feature will work only if the server can decompress the request and you have enabled that feature.
You can also shrink the body of a request before it’s relayed to a backend server.
-
In a
frontend,backend,listenordefaultssection, add the properfilterdirective depending on the load balancer version:- HAProxy 3.4 and newer: Use
filter comp-reqto compress requests orfilter comp-resto compress responses. The examples in this section use this syntax. - HAProxy Enterprise all versions/HAProxy 3.3 and older: Use the
filter compressionandcompression directiondirectives. This syntax is deprecated and will become unsupported in a future release.
Example for HAProxy 3.4 and newer:
haproxybackend web_serversfilter comp-reqfilter comp-reshaproxybackend web_serversfilter comp-reqfilter comp-res - HAProxy 3.4 and newer: Use
-
Add the
compression algodirective and set it to one of the following:deflategzipidentityraw-deflate
You can choose different compression algorithms for responses and requests:
- For requests, add an
algo-reqdirective. - For responses, add an
algo-resdirective.
haproxybackend web_serversfilter comp-reqfilter comp-rescompression algo-req deflatecompression algo-res gziphaproxybackend web_serversfilter comp-reqfilter comp-rescompression algo-req deflatecompression algo-res gzipResponses and requests will be compressed only if they aren’t already compressed.
-
Specify the media types to compress.
- If you’re compressing requests, add a
compression type-reqdirective. - If you’re compressing responses, add a
compression type-resdirective.
You can specify different media types for requests and responses.
haproxybackend web_serversfilter comp-reqfilter comp-rescompression algo-req deflatecompression type-req application/json text/xml text/plaincompression algo-res gzipcompression type-res text/css text/html text/plain text/xml text/javascript application/javascript application/jsonhaproxybackend web_serversfilter comp-reqfilter comp-rescompression algo-req deflatecompression type-req application/json text/xml text/plaincompression algo-res gzipcompression type-res text/css text/html text/plain text/xml text/javascript application/javascript application/json - If you’re compressing requests, add a
-
Optional: Add a
compression offloaddirective, which states that the load balancer will remove theAccept-Encodingheader before forwarding a request to backend servers. This prevents the servers from performing compression, offloading that work to the load balancer. Thecompression offloaddirective may only be placed infrontend,listen, andbackendsections:haproxybackend web_serversfilter comp-reqfilter comp-rescompression algo-req deflatecompression type-req application/json text/xml text/plaincompression algo-res gzipcompression type-res text/css text/html text/plain text/xml text/javascript application/javascript application/jsoncompression offloadhaproxybackend web_serversfilter comp-reqfilter comp-rescompression algo-req deflatecompression type-req application/json text/xml text/plaincompression algo-res gzipcompression type-res text/css text/html text/plain text/xml text/javascript application/javascript application/jsoncompression offload
Process-level settings Jump to heading
Add any of the following directives to the global section to customize compression:
| Directive | Description |
|---|---|
maxcomprate <kb> |
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 won’t compress at all. If the maximum isn’t reached, the compression level will be increased up to tune.comp.maxlevel. A value of zero means there isn’t a limit. This is the default value. |
maxcompcpuusage <percent> |
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 <level> |
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. |
Set a minimum file size Jump to heading
This section applies to:
- HAProxy 3.2 and newer
- HAProxy Enterprise 3.2r1 and newer
- HAProxy ALOHA 17.5 and newer
You can set a minimum file size for HTTP compression, compressing files only if they’re large enough to matter. By avoiding unnecessary compression work, you reduce CPU overhead. For instance, 1500 bytes is the default Ethernet Maximum Transmission Unit (MTU) size. A message that already fits into a single MTU will be transmitted quickly without needing compression.
-
For compressing responses, set
compression minsize-resto a number of bytes.haproxybackend web_servers...compression minsize-res 1500haproxybackend web_servers...compression minsize-res 1500 -
For compressing requests, set
compression minsize-reqto a number of bytes.haproxybackend web_servers...compression minsize-req 1500haproxybackend web_servers...compression minsize-req 1500
See also Jump to heading
For complete syntax and usage information on compression, see Compression reference in the HAProxy Configuration Manual.
- To limit CPU usage by compression, see maxcompcpuusage.
- To limit the compression rate per process, see maxcomprate.
- To set the maximum compression level, see tune.comp.maxlevel.