Core concepts

Backends

A backend section defines a pools of servers to which the load balancer will route requests.

You can add as many backend sections as needed. Each backend keyword is followed by a label, such as web_servers, to differentiate it from others.

haproxy
backend web_servers
mode http
balance roundrobin
server s1 192.168.1.25:80 check
server s2 192.168.1.26:80 check
server s3 192.168.1.27:80 check
haproxy
backend web_servers
mode http
balance roundrobin
server s1 192.168.1.25:80 check
server s2 192.168.1.26:80 check
server s3 192.168.1.27:80 check

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 example Jump to heading

The following example shows a pool of servers that are defined within the backend named web_servers. The server keyword identifies each server’s IP address and port, which the load balancer will use to send traffic to that server. The basic syntax of a server line includes a unique name, an IP address, and a port.

haproxy
frontend myfrontend
mode http
bind :80
default_backend web_servers
backend web_servers
mode http
balance roundrobin
server myfirstserver 192.168.1.25:80 check
server mysecondserver 192.168.1.26:80 check
server mythirdserver 192.168.1.27:80 check
haproxy
frontend myfrontend
mode http
bind :80
default_backend web_servers
backend web_servers
mode http
balance roundrobin
server myfirstserver 192.168.1.25:80 check
server mysecondserver 192.168.1.26:80 check
server mythirdserver 192.168.1.27:80 check

Define multiple backends Jump to heading

You can add multiple backend sections to service traffic for multiple websites or applications. In the configuration sample below, frontend foo_and_bar listens for all incoming HTTP requests and uses the use_backend directive to route traffic to either foo_servers or bar_servers, depending on the host HTTP header.

haproxy
frontend foo_and_bar
mode http
bind :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
balance roundrobin
server foo1 192.168.1.25:80 check
server foo2 192.168.1.26:80 check
server foo3 192.168.1.27:80 check
backend bar_servers
mode http
balance roundrobin
server bar1 192.168.1.35:80 check
server bar2 192.168.1.36:80 check
server bar3 192.168.1.37:80 check
haproxy
frontend foo_and_bar
mode http
bind :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
balance roundrobin
server foo1 192.168.1.25:80 check
server foo2 192.168.1.26:80 check
server foo3 192.168.1.27:80 check
backend bar_servers
mode http
balance roundrobin
server bar1 192.168.1.35:80 check
server bar2 192.168.1.36:80 check
server bar3 192.168.1.37:80 check

Change the load balancing algorithm Jump to heading

By default, servers in the backend share the work of handling requests by using round-robin load-balancing. In the next example, we have set the balance directive and set to leastconn, which will send traffic to the server with the fewest number of connections.

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

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

Enable health checks Jump to heading

It’s a good practice to add the check argument to each server line. This enables health checking, which will remove unhealthy servers, which are servers that do not respond to a TCP connection, from the load balancing rotation.

haproxy
backend web_servers
mode http
balance roundrobin
server s1 192.168.1.25:80 check
server s2 192.168.1.26:80 check
server s3 192.168.1.27:80 check
haproxy
backend web_servers
mode http
balance roundrobin
server s1 192.168.1.25:80 check
server s2 192.168.1.26:80 check
server s3 192.168.1.27:80 check

See also Jump to heading

Do you have any suggestions on how we can improve the content of this page?