Core concepts
Backends
A backend
section defines a pool 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_serversmode httpbalance roundrobinserver s1 192.168.1.25:80 checkserver s2 192.168.1.26:80 checkserver s3 192.168.1.27:80 check
haproxy
backend web_serversmode httpbalance roundrobinserver s1 192.168.1.25:80 checkserver s2 192.168.1.26:80 checkserver 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 myfrontendmode httpbind :80default_backend web_serversbackend web_serversmode httpbalance roundrobinserver myfirstserver 192.168.1.25:80 checkserver mysecondserver 192.168.1.26:80 checkserver mythirdserver 192.168.1.27:80 check
haproxy
frontend myfrontendmode httpbind :80default_backend web_serversbackend web_serversmode httpbalance roundrobinserver myfirstserver 192.168.1.25:80 checkserver mysecondserver 192.168.1.26:80 checkserver 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_barmode httpbind :80use_backend foo_servers if { req.hdr(host) -i foo.com }use_backend bar_servers if { req.hdr(host) -i bar.com }backend foo_serversmode httpbalance roundrobinserver foo1 192.168.1.25:80 checkserver foo2 192.168.1.26:80 checkserver foo3 192.168.1.27:80 checkbackend bar_serversmode httpbalance roundrobinserver bar1 192.168.1.35:80 checkserver bar2 192.168.1.36:80 checkserver bar3 192.168.1.37:80 check
haproxy
frontend foo_and_barmode httpbind :80use_backend foo_servers if { req.hdr(host) -i foo.com }use_backend bar_servers if { req.hdr(host) -i bar.com }backend foo_serversmode httpbalance roundrobinserver foo1 192.168.1.25:80 checkserver foo2 192.168.1.26:80 checkserver foo3 192.168.1.27:80 checkbackend bar_serversmode httpbalance roundrobinserver bar1 192.168.1.35:80 checkserver bar2 192.168.1.36:80 checkserver 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_serversmode httpbalance leastconnserver s1 192.168.1.25:80 checkserver s2 192.168.1.26:80 checkserver s3 192.168.1.27:80 check
haproxy
backend web_serversmode httpbalance leastconnserver s1 192.168.1.25:80 checkserver s2 192.168.1.26:80 checkserver 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_serversmode httpbalance roundrobinserver s1 192.168.1.25:80 checkserver s2 192.168.1.26:80 checkserver s3 192.168.1.27:80 check
haproxy
backend web_serversmode httpbalance roundrobinserver s1 192.168.1.25:80 checkserver s2 192.168.1.26:80 checkserver 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?