HAProxy Enterprise Documentation 2.3r1

Backend

HAProxy Enterprise frontend sections accept incoming connections that can then be forwarded to a pool of servers. The backend section is where those pools of servers that will service requests are defined.

You may add as many backend sections as needed. Each backend keyword is followed by a label, such as www.example.com, to differentiate it from others.

backend www.example.com
   # Your configuration directives

The label is mostly for readability, but it does come into play when referencing stick tables and categorizing traffic metrics. It should consist of only upper or lowercase letters, digits, dashes, underscores, dots, and colons.

Backend configuration examples

The following example defines a frontend with the label myfrontend that uses the default_backend directive to forward incoming traffic to the pool of servers that are defined in the backend web_servers. The server keyword is used here to define each server on a separate line. The basic syntax is server, followed by a unique name, and the IP and port of the backend server.

frontend myfrontend
   mode http
   bind *:80
   default_backend web_servers

backend web_servers
   mode http
   server s1 192.168.1.25:80
   server s2 192.168.1.26:80
   server s3 192.168.1.27:80

Defining multiple backends

Multiple backend sections can be added to service traffic for different websites or applications. In the configuration sample bellow frontend foo_and_bar listens for all incoming http requests and uses the use_backend directive to forward traffic to the foo_servers and bar_servers.

frontend foo_and_bar
   mode http
   bind 192.168.1.5:80
   use_backend foo_servers if { req.hdr(host) -i foo.com }
   use_backend bar_servers if { req.hdr(host) -i bar.com  }

backend foo_servers
   mode http
   server s1 192.168.1.25:80
   server s2 192.168.1.26:80
   server s3 192.168.1.27:80

backend bar_servers
   mode http
   server s1 192.168.1.35:80
   server s2 192.168.1.36:80
   server s3 192.168.1.37:80

Load balancing algorithms

By default, requests are sent to the pool of servers using round-robin load balancing. In the next example we have added the balance directive and set the load balancing mode to leastconn which will send traffic to the server with the lowest number of connections.

backend web_servers
   mode http
   balance leastconn
   server s1 192.168.1.25:80
   server s2 192.168.1.26:80
   server s3 192.168.1.27:80

Other available load balancing algorithms include static-rr, source, first, random, and more.

Health checking

It is a generally good practice to add the check field to each server line. This enables TCP-layer health checking, which will remove unhealthy servers -- servers that do not respond to a TCP connection -- from the load balancing rotation. Add option httpchk to switch on HTTP-layer health checking, which sends an HTTP request to verify responsiveness of the servers:

backend web_servers
   mode http
   balance roundrobin

   # Enable HTTP health checks
   option httpchk

   server s1 192.168.1.25:80 check
   server s2 192.168.1.26:8080 check
   server s3 192.168.1.27:8080 check

Next up

Listen