Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HAproxy domain name to backend mapping for path(url) based routing

Tags:

haproxy

Does HAProxy support domain name to backend mapping for path based routing.

Currently it does support maps for vhost:

frontend xyz
   <other_lines>
   use_backend backend1 if { hdr(Host) -i myapp.domain1.com }
   use_backend backend2 if { hdr(Host) -i myapp.domain2.com }

Can be rewritten using maps as:

frontend xyz
   <other_lines>
   use_backend %[req.hdr(host),lower,map_dom(/path/to/map,default)]

With the contents of map file as:

#domainname  backendname
myapp.domain1.com backend1
myapp.domain2.com backend2

But if the routing is based on paths as shown in the example below:

frontend xyz
   acl host_server_myapp hdr(host) -i myapp.domain.com
   acl path_path1 path_beg /path1
   acl path_path2 path_beg /path2
   use_backend backend1 if host_server_myapp path_path1
   use_backend backend2 if host_server_myapp path_path2

Is it possible to have mapping for this usecase? Using base instead of hdr(host) might give the entire path but it will not have the flexibility of domains since base is string comparison. Is there an other way to convert this to haproxy maps.

like image 395
ajays20078 Avatar asked Jun 16 '16 11:06

ajays20078


People also ask

Can HAProxy have multiple frontends?

When HAProxy Enterprise is used as a reverse proxy in front of your backend servers, a frontend section defines the IP addresses and ports that clients can connect to. You may add as many frontend sections as needed to expose various websites or applications to the internet.

Can you use HAProxy as a forward proxy?

In this presentation, Julien Pivotto explains how Inuits uses HAProxy in an unconventional way: as a forward proxy to route outgoing traffic. This unique use case has uncovered a trove of useful features within HAProxy.

Can HAProxy used as API gateway?

HAProxy, the world's fastest and most widely used software load balancer, fills the role as an API gateway extremely well. In addition to routing API calls to the proper backend servers, it also handles load balancing, security, rate limiting, caching, monitoring, and other cross-cutting concerns.

What is frontend and backend in HAProxy?

The frontend is the node by which HAProxy listens for connections. Backend nodes are those by which HAProxy can forward requests. A third node type, the stats node, can be used to monitor the load balancer and the other two nodes.


1 Answers

Start with the Layer 7 base fetch --

This returns the concatenation of the first Host header and the path part of the request, which starts at the first slash and ends before the question mark.

...then use map_beg() to match the beginning of the string to the map.

use_backend %[base,map_beg(/etc/haproxy/testmap.map,default)]

If the map file /etc/haproxy/testmap.map has a line matching the prefix, the backend in the map file is used. Otherwise, the backend called default will be used (that's the 2nd argument to map_beg() -- the value to be returned if the map doesn't match).

If the resulting backend doesn't actually exist, HAProxy continues processing the request as if this statement weren't configured at all.

So your map file would look something like this:

example.com/foo     this-backend # note, also matches /foo/ba 
example.com/foo/bar that-backend # note, matches /foo/bar
example.org/foo     some-other-backend

To treat a subdomain as equivalent to the parent domain (e.g., treating example.com and www.example.com to be handled equivalently, without map duplication, as discussed in comments) the regsub() converter could be used to modify the value passed to the map:

use_backend %[base,regsub(^www\.,,i),map_beg(/etc/haproxy/testmap.map,default)]
like image 194
Michael - sqlbot Avatar answered Sep 16 '22 12:09

Michael - sqlbot