Syntax
An example map file, routes.map:
www.example.com webservers
api.example.com apiservers
static.example.com cacheservers
Things to know:
A map file is a simple text file.
Each line begins with a key, followed by a space, then the value.
The key must not have spaces, but the value can have spaces.
Empty lines are ignored.
Comments begin with a hash sign and must be on their own line.
HAProxy Enterprise loads the file only during startup or a reload.
Use a map file
You access a map file by using the map
converter. Its first argument is the path to the map file. Its second parameter sets a default value and is optional.
In the following example, we use the req.hdr
fetch method to get the Host request header and then pass it to the map
converter to look up the matching key in the file routes.map. If a matching key exists in the file, HAProxy Enterprise returns its value (i.e. apiservers). Otherwise, it uses the default value webservers.
Example map file routes.map:
www.example.com webservers api.example.com apiservers static.example.com cacheservers
HAProxy Enterprise configuration:
frontend www bind :80 # Choose which backend depending on the Host header use_backend %[req.hdr(host),map(/etc/hapee-1.8/routes.map,webservers)] backend webservers server s1 192.168.50.10:80 check backend apiservers server s1 192.168.50.20:80 check backend cacheservers server s1 192.168.50.30:80 check
If you do not set a default, then HAProxy Enterprise does not return a value from the map
converter if there is no match found within the file.
Map converter variants
Variants of the map
converter allow a partial match of a key.
Match the beginning of a sample
The map_beg
converter matches the key against the beginning of the input sample returned by the fetch. For instance, a requested URL path of /api/weather/midwest would match /api/ in the file.
Map file routes.map:
/api/ apiservers /static/ cacheservers
HAProxy Enterprise configuration:
frontend www bind :80 # Matches the beginning of the input, but defaults to webservers use_backend %[path,map_beg(/etc/hapee-1.8/routes.map,webservers)]
Match the end of a sample
The map_end
converter matches the key against the end of the input sample. For instance, a URL path /image.jpg would match .jpg in the file.
Map file routes.map:
.jpg cacheservers .png cacheservers
HAProxy Enterprise configuration:
frontend www bind :80 # Matches the end of the input, but defaults to webservers use_backend %[path,map_end(/etc/hapee-1.8/routes.map,webservers)]
Match a substring of a sample
The map_sub
converter matches a substring in the input sample. For instance, a URL path /shop/shirts/sale/ would match /shirts/ in the file.
Map file routes.map:
/shirts/ webservers1 /pants/ webservers2
HAProxy Enterprise configuration:
frontend www bind :80 # Matches a substring in the input, but defaults to webservers1 use_backend %[path,map_sub(/etc/hapee-1.8/routes.map,webservers1)]
Match an IP address
The map_ip
converter matches an IP address. If a key in the map file has a netmask (e.g. 192.168.0.0/16), then the IP matches if it falls within the IP range.
Map file routes.map:
10.0.0.0/16 eastcoast 10.1.0.0/16 westcoast
HAProxy Enterprise configuration:
frontend www bind :80 # Matches an IP addres in a range, but defaults to eastcoast use_backend %[src,map_ip(/etc/hapee-1.8/routes.map,eastcoast)]
Match a domain
The map_dom
converter matches a domain or a part of a domain against a key. For instance, the key example.com would match input samples example.com, www.example.com, or demo.example.com.
Map file routes.map:
example.com webservers1 test.com webservers2
HAProxy Enterprise configuration:
frontend www bind :80 # Matches an IP addres in a range use_backend %[req.hdr(host),map_dom(/etc/hapee-1.8/routes.map)]
Match a regular expression
The map_reg
converter matches the input sample based on a regular expression. For instance, the regular expression ^(\/sale\/).*(\.jpg)$
would match a URL that begins with /sale/ and ends with .jpg.
Map file routes.map:
^(\/sale\/).*(\.jpg)$ cacheservers
HAProxy Enterprise configuration:
frontend www bind :80 # Matches the regular expression, but defaults to webservers use_backend %[path,map_reg(/etc/hapee-1.8/routes.map,webservers)]
Next up
Stick Tables