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 generally a 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
Data Plane API
You can manage backends remotely by calling the Data Plane API endpoint /services/haproxy/configuration/backends.
-
Get a list of backends with the GET method:
$ curl -X GET \ --user admin:adminpwd \ "http://127.0.0.1:5555/v2/services/haproxy/configuration/backends" # output { "_version":2, "data":[{ "name":"webservers" }] }
#.Add a backend by using the POST method (note that servers are added separately with the /services/haproxy/configuration/servers endpoint):
$ curl -X POST \ --user admin:adminpwd \ -H "Content-Type: application/json" \ -d '{ "name": "myservers", "mode":"http", "balance": { "algorithm":"roundrobin" }, "default_server": { "alpn": "h2", "check": "enabled", "check_alpn": "h2", "maxconn": 30, "no_sslv3": "enabled", "weight": 100 } }' \ "http://127.0.0.1:5555/v2/services/haproxy/configuration/backends?version=2"
-
Get information about a specific backend by adding its name to the GET call:
$ curl -X GET \ --user admin:adminpwd \ "http://127.0.0.1:5555/v2/services/haproxy/configuration/backends/myservers" # output { "_version":3, "data":{ "balance":{ "algorithm":"roundrobin" }, "default_server":{ "alpn":"h2", "check":"enabled", "check_alpn":"h2", "maxconn":30, "proxy-v2-options":null, "weight":100}, "mode":"http", "name":"myservers" } } }
-
Replace an existing backend by calling PUT:
$ curl -X PUT \ --user admin:adminpwd \ -H "Content-Type: application/json" \ -d '{ "name": "myservers", "mode":"http", "balance": { "algorithm":"roundrobin" }, "default_server": { "alpn": "h2", "check": "enabled", "check_alpn": "h2", "maxconn": 30, "no_sslv3": "enabled", "weight": 50 } }' \ "http://127.0.0.1:5555/v2/services/haproxy/configuration/backends/myservers?version=3"
-
Delete a backend with the DELETE method:
$ curl -X DELETE \ --user admin:adminpwd \ "http://127.0.0.1:5555/v2/services/haproxy/configuration/backends/myservers?version=4"
Next up
Listen