Routing Domain Name in Haproxy

0
4068

Haproxy is an open-source software that provides high availability, load balancing and proxying for applications such as TCP and HTTP. It is widely used in web hosting environments and other scenarios where high availability and load balancing are important. One of the key features of Haproxy is its ability to route traffic based on domain names. In this article, we will show you how to set up domain name routing in Haproxy.

What is haproxy routing?

Before delving into domain name routing, it is Important to understand how Haproxy handles traffic routing in general. Haproxy uses configuration files to understand the behavior of the software. These configuration files contain sections for frontend and backend behavior, with the frontend section representing and managing incoming traffic. While the backend section shows the backend servers to which the traffic is routed.

Routing Domain Name In Haproxy

Routing traffic based on domain name is a common use for Haproxy. It allows you to run multiple websites or applications on the same server or set of servers, but route traffic based on the domain name used to access each site. To accomplish this, you must configure Haproxy to listen for traffic on the appropriate ports and domain names, and then route that traffic to the appropriate backend servers.

I hope you are following the below guide. Now suppose you want to route domain name to open specific backend that have your configuration file.

How to route domain name

Find

frontend https
   mode http
   bind *:443 ssl crt /etc/letsencrypt/live/web.bullten.work/web.bullten.work.pem crt /etc/letsencrypt/live/haproxy1.bullten.work/haproxy1.bullten.work.pem alpn h2,http/1.1


   option forwardfor

    http-request track-sc0 src table per_ip_rates
    http-request deny deny_status 429 if { sc_http_req_rate(0) gt 100 }


    acl from_cf    src -f /etc/haproxy/cloudflare_ips.lst
    http-request set-src req.hdr(CF-Connecting-IP) if from_cf

   default_backend app-main

Now at the end add

    acl host_bacon hdr(host) -i haproxy1.bullten.work

    use_backend bacon_cluster if host_bacon

So it will look like. Replace haproxy1.bullten.work with your domain name

frontend https
   mode http
   bind *:443 ssl crt /etc/letsencrypt/live/web.bullten.work/web.bullten.work.pem alpn h2,http/1.1


   option forwardfor

    http-request track-sc0 src table per_ip_rates
    http-request deny deny_status 429 if { sc_http_req_rate(0) gt 100 }


    acl from_cf    src -f /etc/haproxy/cloudflare_ips.lst
    http-request set-src req.hdr(CF-Connecting-IP) if from_cf


    acl host_bacon hdr(host) -i haproxy1.bullten.work
    use_backend bacon_cluster if host_bacon


   default_backend app-main

Now create a backend bacon_cluster

backend bacon_cluster
    mode http
    balance roundrobin                                     #Balance algorithm
    option httpchk HEAD / HTTP/1.1\r\nHost:\ localhost    #Check the server application is up and healty - 200 status code
    timeout queue 10s
    server nginx1 192.168.1.1:443 check  ssl verify none weight 3      #Nginx1

This will tell to haproxy to point to bacon_cluster when domain entered is haproxy1.bullten.work

Now just setup SSL. Replace haproxy1.bullten.work with your domain name.

frontend https
   mode http
   bind *:443 ssl crt /etc/letsencrypt/live/web.bullten.work/web.bullten.work.pem crt /etc/letsencrypt/live/haproxy1.bullten.work/haproxy1.bullten.work.pem alpn h2,http/1.1


   option forwardfor

    http-request track-sc0 src table per_ip_rates
    http-request deny deny_status 429 if { sc_http_req_rate(0) gt 100 }


    acl from_cf    src -f /etc/haproxy/cloudflare_ips.lst
    http-request set-src req.hdr(CF-Connecting-IP) if from_cf


    acl host_bacon hdr(host) -i haproxy1.bullten.work
    use_backend bacon_cluster if host_bacon


   default_backend app-main