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
-
In a
frontend
,backend
,listen
ordefaults
section, add thefilter compression
andcompression algo
directives. Setcompression algo
to one of the following:deflate
gzip
identity
raw-deflate
haproxybackend web_serversfilter compressioncompression algo gziphaproxybackend web_serversfilter compressioncompression algo gzipResponses will be compressed only if they are not already compressed.
-
Add a
compression type
directive, which specifies the MIME types to compress:haproxybackend web_serversfilter compressioncompression algo gzipcompression type text/css text/html text/javascript application/javascript text/plain text/xml application/jsonhaproxybackend web_serversfilter compressioncompression algo gzipcompression type text/css text/html text/javascript application/javascript text/plain text/xml application/json -
Optional: Add a
compression offload
directive, which states that the load balancer will remove theAccept-Encoding
header before forwarding a request to backend servers, which prevents the servers from performing compression, offloading that work to the load balancer. Thecompression offload
directive may only be placed infrontend
,listen
, andbackend
sections:haproxybackend web_serversfilter compressioncompression algo gzipcompression type text/css text/html text/javascript application/javascript text/plain text/xml application/jsoncompression offloadhaproxybackend web_serversfilter compressioncompression 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
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.
-
In a
frontend
,backend
,listen
ordefaults
section, add thefilter compression
andcompression direction
directives. Set thecompression direction
to one of:request
to compress only requestsresponse
to compress only responsesboth
to compress both requests and responses
haproxybackend web_serversfilter compressioncompression direction bothhaproxybackend web_serversfilter compressioncompression direction both -
Specify the compression algorithm:
- If you set
compression direction
torequest
orboth
, add analgo-req
directive. - If you set
compression direction
toresponse
orboth
, add analgo-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.
haproxybackend web_serversfilter compressioncompression direction bothcompression algo-req deflatecompression algo-res gziphaproxybackend web_serversfilter compressioncompression direction bothcompression algo-req deflatecompression algo-res gzipResponses and requests will be compressed only if they are not already compressed:
- If you set
-
Specify the MIME types to compress for requests and / or responses depending on which
compression direction
you specified:- If you set
compression direction
torequest
orboth
, add acompression type-req
directive. - If you set the
compression direction
toresponse
orboth
, add acompression type-res
directive.
You can specify different MIME types for requests and responses.
haproxybackend web_serversfilter compressioncompression direction bothcompression 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 compressioncompression direction bothcompression 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 set
-
Optional: Add a
compression offload
directive, which states that the load balancer will remove theAccept-Encoding
header before forwarding a request to backend servers, which prevents the servers from performing compression, offloading that work to the load balancer. Thecompression offload
directive may only be placed infrontend
,listen
, andbackend
sections:haproxybackend web_serversfilter compressioncompression direction bothcompression 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 compressioncompression direction bothcompression 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 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?