Learn How To Installing HAProxy 1.7 on Debian 9.1 (Stretch)

January 29, 2020

Table of Contents

If you are using a different system, please check our other tutorials.

HAProxy is a network software application that offers high availability, load balancing, and proxying for TCP and HTTP network applications. It is suited for high traffic websites, and powers many popular sites across the web. This article will show you how to install and configure HAProxy on Debian 9.1.

Although HAProxy has several prominent features, this article focuses on how to setup HAProxy to “proxy” your web application.


  • At least two IT Web Services servers (for load balancing functionality) with your website or web application deployed to both of them.

Installing HAProxy

Debian 9 already ships with HAProxy 1.7 (latest stable release at time of writing), and we can simply install it using apt-get:

# apt-get update
# apt-get install haproxy

If the previous commands were successful, then you have installed HAProxy and you can proceed to the next step.

Configuring HAProxy

The HAProxy configuration file is split up into two sections — “global” and “proxies”. One deals with process-wide configuration, while the latter consists of default configuration, frontend, and backend sections.

Global Section

Using your favorite text editor, open /etc/haproxy/haproxy.cfg and notice the predefined sections: “global” and “defaults”. The first thing that you may want to do is increase the maxconn to a reasonable size, as this affects the connections that HAProxy allows. Too many connections may cause your web service to crash due to many requests. You will need to adjust the size to see what works for you. In the global section, we have chosen a maxconn value of 3072.

    maxconn 3072

In the default section, add the following line under mode http:

option forwardfor

This will add X-Forwarded-For headers to each request, which allows your backend servers to learn the original IP address of the user.

Also, add this line to enable HTTP connection-close mode on the server side while keeping the ability to support HTTP keep-alive on the client side. This reduces latency on the client side and helps conserve server resources:

option http-server-close

If you wish to use keep-alive on both the client and server sides, then you could use option http-keep-alive instead. This option is particularly useful when the cost of establishing a new connection to the server is significant compared to the cost of retrieving the requested resource.

Finally, the resulting config file will look something like this:

    mode http
    option forwardfor
    option http-server-close
    timeout connect 5000ms
    timeout client 50000ms
    timeout server 50000ms

Proxies Section

To set up your proxy, you will need to add two sections to the configuration file to define the two parts of the proxy: the frontend and the backend.

Frontend Configuration

The frontend will handle your HTTP connections. Add the following to the end of your haproxy.cfg file:

frontend http-frontend
    bind public_ip:80
    reqadd X-Forwarded-Proto: http
    default_backend wwwbackend

Be sure to replace public_ip with your server’s public IP address or domain name.

Backend Configuration

Setup your backend by adding the following lines to the end of your configuration file:

backend wwwbackend
    server 1-www server1_ip:80 check
    server 2-www server2_ip:80 check
    server 3-www server3_ip:80 check

The backend configuration used here creates 3 connections named X-www. (X is 1, 2 or 3.) Each one of them corresponds to a serverX_ip:80 address. (Replace serverX_ip with your IT Web Services instances’ IP addresses.) This will allow you to load balance between each server in the specified server set (assuming each IP address corresponds to a different server). The check option makes the load balancer perform health checks on the server.

Save the configuration file, and then restart HAProxy:

service haproxy restart

If everything is working, then you will be able to connect to http://public_ip/ (replacing it with your public IP or domain name as configured in the frontend step) and view your website.

Debugging Errors

If your HAProxy instance refuses to start after your modifications, chances are that you have an error somewhere in the configuration file. To get clear messages about the issue in the configuration file, you can try to start HAProxy manually using this command:

# haproxy -f /etc/haproxy/haproxy.cfg

For instance, if you see output like this:

[ALERT] 234/195612 (2561) : parsing [/etc/haproxy/haproxy.cfg:48] : server 1-www has neither service port nor check port nor tcp_check rule 'connect' with port information. Check has been disabled.
[ALERT] 234/195612 (2561) : Error(s) found in configuration file : /etc/haproxy/haproxy.cfg
[ALERT] 234/195612 (2561) : Fatal errors found in configuration.

Then, you have forgotten to specify the port number for the server 1-www.

Need help?

Do you need help setting up this on your own service?
Please contact us and we’ll provide you the best possible quote!