Search code examples
linuxansiblewebserverhaproxy

Linux Haproxy loadbalancer haproxy.cfg syntax for 2 webservers


I have a question regarding this syntax in the file haproxy.cfg when setting up a loadbalancer for 2 webservers. I have to distribute the weight (70/20) over 2 webservers over the port 80. Is this correct?

And do i need to specify the {{ Port }} or not?

Management server -> Loadbalancer -> 2 webservers

Is this port 80 only on the left side, right side or both sides?

(file: roles/loadbalancer/templates/haproxy.cfg)

# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main
    bind {{ Port }}
    acl url_static       path_beg       -i /static /images /javascript /stylesheets
    acl url_static       path_end       -i .jpg .gif .png .css .js

    use_backend static          if url_static
    default_backend             app

#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
    balance     roundrobin
    server      static 127.0.0.1:4331 check

#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
    balance     roundrobin
    server  {{ server1Name }} {{ server1IP }} check weight 70
    server  {{ server2Name }} {{ server2IP }} check weight 30
#    server  app3 127.0.0.1:5003 check
#    server  app4 127.0.0.1:5004 check

(file: roles/loadbalancer/vars/main.yml)

---
# vars file for roles/loadbalancer
Port: "{{ hostvars['node1']['ansible_port'] }}"
server1Name: "{{ hostvars['webnode1']['inventory_hostname'] }}"
server1IP: "{{ hostvars['webnode1']['ansible_host'] }}"
server2Name: "{{ hostvars['webnode2']['inventory_hostname'] }}"
server2IP: "{{ hostvars['webnode2']['ansible_host'] }}"

Solution

  • Yes in the server line have the port be there after the ip.

    server  {{ server1Name }} {{ server1IP }}:{{server1port}} check weight 70
    

    You can find here the full documentation of the server keyword