Linux Haproxy 负载均衡器 haproxy.cfg 2 个 Web 服务器的语法

Linux Haproxy loadbalancer haproxy.cfg syntax for 2 webservers

在为 2 个网络服务器设置负载平衡器时,我对文件 haproxy.cfg 中的语法有疑问。我必须通过端口 80 将权重 (70/20) 分配给 2 个网络服务器。 这是正确的吗?

我是否需要指定{{端口}}?

管理服务器 -> 负载均衡器 -> 2 个网络服务器

这个80端口是只在左侧,右侧还是两侧?

(文件: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

(文件: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'] }}"

是的,在服务器行中,端口在 ip 之后。

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

您可以在此处找到 server 关键字的完整文档