HAproxy – backend and domain management using map

This is quick write up how to use single line for easy backend mapping within HAproxy. This has been showed to me by my buddy while challenging current configuration which started to grow.

The first thing you will need to have is to create a map file. Its structure its simple – first column is what comes is , second is what comes out. So for our domain mapping we can have file with domain name and respective backend i.e.

domain.com backend_com
www.domain.com backend.com

Next is just configuration line on your front end associating domains with backends based on the host header

use_backend %[req.hdr(host),lower,map_dom(/etc/haproxy/<PATH-TO-MAP-FILE>,<DEFAULT-BACKEND>)]


And that is it 🙂 you have now got your self really dynamic configuration




  1. That looks really convenient and quick configuration method.
    What about multiple backends? Is there any example also for tcp frontend?

    • Hi,

      Havent tested for TCP based backends but I think it would follow the same principal. The solution shown here worked without any problems with multibackend environment by just having multiple entries in the map file.

Leave a Reply

Your email address will not be published. Required fields are marked *