设置 haproxy 和清漆时出错

Red*_*ant 1 varnish haproxy

我一直在关注 haproxy 博客上关于如何使用 haproxy 安装清漆的教程。(链接) 这部分的 haproxy 配置设置有问题:

frontend ft_web_static
  bind 10.0.1.3:80
  monitor-uri /haproxycheck
  # Tells Varnish to stop asking for static content when servers are dead
  # Varnish would deliver staled content
  monitor fail if nbsrv(bk_appsrv_static) eq 0
  default_backend bk_appsrv_static
Run Code Online (Sandbox Code Playgroud)

当我重新启动 haproxy 时,它给了我这个错误:

[root@ch]# service haproxy restart
[ALERT] 348/004936 (28582) : parsing [/etc/haproxy/haproxy.cfg:282] : error detected while parsing a 'monitor fail' condition : no such ACL : 'nbsrv(apache2_static)'.
Run Code Online (Sandbox Code Playgroud)

我猜有什么问题nbsrv(bk_appsrv_static) eq 0。文章后评论区也有人提出了这个问题,但没有人给出解决方案。任何人都可以弄清楚设置中是否有拼写错误或错误?

这是完整的配置:

# On Aloha, the global section is already setup for you
# and the haproxy stats socket is available at /var/run/haproxy.stats
global
  stats socket ./haproxy.stats level admin
  log 10.0.1.10 local3

# default options
defaults
  option http-server-close
  mode http
  log global
  option httplog
  timeout connect 5s
  timeout client 20s
  timeout server 15s
  timeout check 1s
  timeout http-keep-alive 1s
  timeout http-request 10s  # slowloris protection
  default-server inter 3s fall 2 rise 2 slowstart 60s

# HAProxy's stats
listen stats
  bind 10.0.1.3:8880
  stats enable
  stats hide-version
  stats uri     /
  stats realm   HAProxy Statistics
  stats auth    admin:admin

# main frontend dedicated to end users
frontend ft_web
  bind 10.0.0.3:80
  acl static_content path_end .jpg .gif .png .css .js .htm .html
  acl pseudo_static path_end .php ! path_beg /dynamic/
  acl image_php path_beg /images.php
  acl varnish_available nbsrv(bk_varnish_uri) ge 1
  # Caches health detection + routing decision
  use_backend bk_varnish_uri if varnish_available static_content
  use_backend bk_varnish_uri if varnish_available pseudo_static
  use_backend bk_varnish_url_param if varnish_available image_php
  # dynamic content or all caches are unavailable
  default_backend bk_appsrv

# appsrv backend for dynamic content
backend bk_appsrv
  balance roundrobin
  # app servers must say if everything is fine on their side
  # and they can process requests
  option httpchk
  option httpchk GET /appcheck
  http-check expect rstring [oO][kK]
  cookie SERVERID insert indirect nocache
  # Transparent proxying using the client IP from the TCP connection
  source 10.0.1.1 usesrc clientip
  server s1 10.0.1.101:80 cookie s1 check maxconn 250
  server s2 10.0.1.102:80 cookie s2 check maxconn 250

# static backend with balance based on the uri, including the query string
# to avoid caching an object on several caches
backend bk_varnish_uri
  balance uri # in latest HAProxy version, one can add 'whole' keyword
  # Varnish must tell it's ready to accept traffic
  option httpchk HEAD /varnishcheck
  http-check expect status 200
  # client IP information
  option forwardfor
  # avoid request redistribution when the number of caches changes (crash or start up)
  hash-type consistent
  server varnish1 10.0.1.201:80 check maxconn 1000
  server varnish2 10.0.1.202:80 check maxconn 1000

# cache backend with balance based on the value of the URL parameter called "id"
# to avoid caching an object on several caches
backend bk_varnish_url_param
  balance url_param id
  # client IP information
  option forwardfor
  # avoid request redistribution when the number of caches changes (crash or start up)
  hash-type consistent
  server varnish1 10.0.1.201:80 maxconn 1000 track bk_varnish_uri/varnish1
  server varnish2 10.0.1.202:80 maxconn 1000 track bk_varnish_uri/varnish2

# frontend used by Varnish servers when updating their cache
frontend ft_web_static
  bind 10.0.1.3:80
  monitor-uri /haproxycheck
  # Tells Varnish to stop asking for static content when servers are dead
  # Varnish would deliver staled content
  monitor fail if nbsrv(bk_appsrv_static) eq 0
  default_backend bk_appsrv_static

# appsrv backend used by Varnish to update their cache
backend bk_appsrv_static
  balance roundrobin
  # anything different than a status code 200 on the URL /staticcheck.txt
  # must be considered as an error
  option httpchk
  option httpchk HEAD /staticcheck.txt
  http-check expect status 200
  # Transparent proxying using the client IP provided by X-Forwarded-For header
  source 10.0.1.1 usesrc hdr_ip(X-Forwarded-For)
  server s1 10.0.1.101:80 check maxconn 50 slowstart 10s
  server s2 10.0.1.102:80 check maxconn 50 slowstart 10s
Run Code Online (Sandbox Code Playgroud)

Gre*_*egL 5

那篇文章已经超过 3 年了,从那时起已经有 2 个稳定版本的 HAProxy,所以我怀疑这就是它不能逐字运行的原因。

如果我不得不猜测你的确切问题,我会说它与monitor fail语句的语法有关,它期望在if.

你必须选择解决这个问题。

  1. 将 ACL 更改为“匿名”ACL,如HAProxy 文档的第 7.2 节所述。我已经链接了 v1.6,但在 v1.5 中也是一样的。

    monitor会看起来像:

    monitor fail if { nbsrv(bk_appsrv_static) eq 0 }  
    
    Run Code Online (Sandbox Code Playgroud)
  2. bk_appsvr_static后端的整体状态创建一个命名的 ACL ,并将其传递给该monitor fail行。

    那看起来像:

    acl bk_app_static_noservers nbsrv(bk_appsrv_static) eq 0
    monitor fail if bk_app_static_noservers
    
    Run Code Online (Sandbox Code Playgroud)

    在这种情况下,如果bk_appsvr_static没有任何可用的服务器,则 ACLFALSEmonitor fail将适用。