HAProxy and gzip compression

Synopsis

Compression is a Technic to reduce object size to reduce delivery delay for objects over HTTP protocol.
Until now, HAProxy did not include such feature. But the guys at HAProxy Technologies worked hard on it (mainly David Du Colombier and @wlallemand).
HAProxy can now be considered an new option to compress HTTP streams, as well as nginx, apache or IIS which already does it.

Note that this is in early beta, so use it with care.

Compilation


Get the latest HAProxy git version, by running a “git pull” in your HAProxy git directory.
If you don’t already have such directory, then run the a:

git clone http://git.1wt.eu/git/haproxy.git

Once your HAProxy sources are updated, then you can compile HAProxy:

make TARGET=linux26 USE_ZLIB=yes

Configuration

this is a very simple configuration test:

listen ft_web
 option http-server-close
 mode http
 bind 127.0.0.1:8090 name http
 default_backend bk_web

backend bk_web
 option http-server-close
 mode http
 compression algo gzip
 compression type text/html text/plain text/css
 server localhost 127.0.0.1:80

Compression test

On my localost, I have an apache with compression disabled and a style.css object whose size is 16302 bytes.

Download without compression requested

curl -o/dev/null -D - "http://127.0.0.1:8090/style.css" 
HTTP/1.1 200 OK
Date: Fri, 26 Oct 2012 08:55:42 GMT
Server: Apache/2.2.16 (Debian)
Last-Modified: Sun, 11 Mar 2012 17:01:39 GMT
ETag: "a35d6-3fae-4bafa944542c0"
Accept-Ranges: bytes
Content-Length: 16302
Content-Type: text/css

100 16302  100 16302    0     0  5722k      0 --:--:-- --:--:-- --:--:-- 7959k

Download with compression requested

 curl -o/dev/null -D - "http://127.0.0.1:8090/style.css" -H "Accept-Encoding: gzip"
HTTP/1.1 200 OK
Date: Fri, 26 Oct 2012 08:56:28 GMT
Server: Apache/2.2.16 (Debian)
Last-Modified: Sun, 11 Mar 2012 17:01:39 GMT
ETag: "a35d6-3fae-4bafa944542c0"
Accept-Ranges: bytes
Content-Type: text/css
Transfer-Encoding: chunked
Content-Encoding: gzip

100  4036    0  4036    0     0  1169k      0 --:--:-- --:--:-- --:--:-- 1970k

In this example, object size passed from 16302 bytes to 4036 bytes.

Have fun !!!!

Links

10 thoughts on “HAProxy and gzip compression”

  1. I’m really impressed with the progress HAProxy is making working thanks to you guys. Its all really impressive stuff.

    So how will this influence situations where Nginx sits behind HAProxy already gzip’ing the content and providing a gzip’ed stream? As far as I am aware, this has always been functional.

    Is the purpose to disable gzip compression on the backends and offload it to the front load balancers?

    1. If you wanted to, you can off-load the compression to the front load balancers, but you don’t have to. By default, HAProxy will not remove encoding acceptance headers, and if backend compresses, HAProxy will just forward it back out to the client. You can additionally instruct HAProxy to always compress, and to remove the header before passing request out to the back end.

  2. Hi,

    i am getting unknown keyword ‘compression’ in ‘backend’ section error while adding compression section using haproxy-1.4.24.

    I have followed the below steps.

    1) make clean
    2) make TARGET=linux26 USE_ZLIB=yes
    3) make install

    [ALERT] 275/142848 (6961) : parsing [/etc/haproxy/haproxy.cfg:41] : unknown keyword ‘compression’ in ‘backend’ section
    [ALERT] 275/142848 (6961) : parsing [/etc/haproxy/haproxy.cfg:42] : unknown keyword ‘compression’ in ‘backend’ section

Leave a Reply

Your email address will not be published. Required fields are marked *