Skip to content

Instantly share code, notes, and snippets.

@haproxytechblog
Last active February 18, 2023 22:49
Show Gist options
  • Save haproxytechblog/07ea608e9e2061412e886b3eebeb8387 to your computer and use it in GitHub Desktop.
Save haproxytechblog/07ea608e9e2061412e886b3eebeb8387 to your computer and use it in GitHub Desktop.
How to Map Domain Names to Backend Server Pools with HAProxy
frontend default
bind :80
# ACL for "example.com" and "www.example.com"
acl ACL_example.com hdr(host) -i example.com www.example.com
use_backend be_example.com if ACL_example.com
# ACL for "example.net"
acl ACL_example.net hdr(host) -i example.net
use_backend be_example.net if ACL_example.net
use_backend be_example_net if ACL_example.net
frontend fe_main
bind :80
# If Host header is api.example.com then use
# api.example.com backend
use_backend %[req.hdr(Host),lower]
backend api.example.com
balance roundrobin
server api1 127.0.0.1:8080 check
server api2 127.0.0.1:8081 check
#domainname backendname
example.com be_default
example.net be_default
api.example.com be_api
api1.example.net be_api1
api2.example.com be_api2
# [...]
api10000.example.com be_api
frontend default
bind :80
use_backend %[req.hdr(host),lower,map_dom(/etc/haproxy/maps/hosts.map,be_default)]
use_backend be_example.com if ACL_example.com
frontend www
bind :80
use_backend %[path,map_beg(/etc/haproxy/maps/routes.map,be_default)]
/api be_api
/login be_auth
stats socket /var/run/haproxy.sock
user haproxy group haproxy mode 660 level admin expose-fd listeners
$ echo "help" | sudo socat stdio /var/run/haproxy.sock | grep map
add map : add map entry
clear map <id> : clear the content of this map
del map : delete map entry
get map : report the keys and values matching a sample for a map
set map : modify map entry
show map [id] : report available maps or dump a map's contents
$ echo "add map /etc/haproxy/maps/hosts.map api.example.com be_api" | socat stdio /var/run/haproxy.sock
$ echo "del map /etc/hapee-2.1/hosts.map api.example.com" | socat stdio /var/run/haproxy.sock
dynamic-update
update id /etc/hapee-2.1/maps/domains.map url http://10.0.0.1/domains.map delay 300s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment