Use acl in backend / shrink the config

haproxyload balancing

In my scenario I must switch servers based on an containing url string &mode=edit or &mode=create between my servers to balance the load. My config is created via script from about 100 different urls/ip. My current way with one frontend and 2 backends is pretty long and the huge amount of health checks are not good for my servers…

How can I put these stuff together in one listeninstead of frontend? I can not figure out how to use ACLs in an listen to determine which server to use.

frontend myFrontend

    bind 127.0.0.1:80
    bind 127.0.0.1:443 ssl crt /etc/haproxy/ssl/myFrontend/myFrontend.pem
    redirect scheme https if !{ ssl_fc }
    use_backend myBackend-edit if { url_sub &mode=edit } or { url_sub &mode=create }
    default_backend myBackend

backend myBackend

    server srv1 1.1.1.1:10201 cookie srv1 ssl check
    server srv2 2.2.2.2:10201 cookie srv1 ssl check backup
    server srv3 3.3.3.3:10201 cookie srv1 ssl check backup

backend myBackend-edit

    server srv1 1.1.1.1:10201 cookie srv1 ssl check backup
    server srv2 2.2.2.2:10201 cookie srv1 ssl check backup
    server srv3 3.3.3.3:10201 cookie srv1 ssl check

Best Answer

the right keyword is use-server (notice that this is an dash and not an underscore like use_backend. Now my Load is reduced and configs are smaller. I only searched for something like use_server...

So this is my working example:

listen myFrontend

    bind 127.0.1.1:80
    bind 127.0.1.1:443 ssl crt /etc/haproxy/ssl/myFrontend/myFrontend.pem
    redirect scheme https if !{ ssl_fc }
    use-server srv2 if { url_sub &mode=edit } or { url_sub &mode=create }
    server srv1 1.1.1.1:10201 cookie srv1 ssl check
    server srv2 2.2.2.2:10201 cookie srv1 ssl check backup
    server srv3 3.3.3.3:10201 cookie srv1 ssl check backup

you can also use load-balancing there and stick with cookies if you like.

source: haproxy manpage | or use the pretty one :)