Last active
February 18, 2023 22:49
-
-
Save haproxytechblog/07ea608e9e2061412e886b3eebeb8387 to your computer and use it in GitHub Desktop.
How to Map Domain Names to Backend Server Pools with HAProxy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use_backend be_example_net if ACL_example.net |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
frontend fe_main | |
bind :80 | |
# If Host header is api.example.com then use | |
# api.example.com backend | |
use_backend %[req.hdr(Host),lower] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
backend api.example.com | |
balance roundrobin | |
server api1 127.0.0.1:8080 check | |
server api2 127.0.0.1:8081 check |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
frontend default | |
bind :80 | |
use_backend %[req.hdr(host),lower,map_dom(/etc/haproxy/maps/hosts.map,be_default)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use_backend be_example.com if ACL_example.com |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
frontend www | |
bind :80 | |
use_backend %[path,map_beg(/etc/haproxy/maps/routes.map,be_default)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/api be_api | |
/login be_auth |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
stats socket /var/run/haproxy.sock | |
user haproxy group haproxy mode 660 level admin expose-fd listeners |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ echo "help" | sudo socat stdio /var/run/haproxy.sock | grep map |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ echo "add map /etc/haproxy/maps/hosts.map api.example.com be_api" | socat stdio /var/run/haproxy.sock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ echo "del map /etc/hapee-2.1/hosts.map api.example.com" | socat stdio /var/run/haproxy.sock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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