You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

HAProxy从1.5.18升级到2.4.17后配置启动失败求助

问题:HAProxy 2.4.17启动失败(从1.5.18配置迁移)

背景

原CentOS 7服务器使用HAProxy 1.5.18,因系统停止维护升级到Red Hat,默认HAProxy版本为2.4.17。将原1.5.18的haproxy.cfg直接用于2.4.17时,执行systemctl start haproxy启动失败。

原HAProxy 1.5.18配置文件

global
   
    log         127.0.0.1 local2
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     5000
    user        haproxy
    group       haproxy
    daemon

tune.maxrewrite 4096
tune.http.maxhdr 202
    
    #tune.ssl.default-dh-param 2048
    tune.ssl.default-dh-param 2048  
    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats
    # utilize system-wide crypto-policies
    ssl-default-bind-ciphers AES256-SHA
    #ssl-default-bind-ciphers PROFILE=SYSTEM
    #ssl-default-server-ciphers PROFILE=SYSTEM

defaults
    mode                    http
    log                     global
    option                  httplog
    option                  forwardfor
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 10
    timeout http-request    40s
    timeout queue           1m
    timeout connect         40s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 60s
    timeout check           50s
    maxconn                 5000

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
#frontend  main *:5000

frontend xyzabc 
#frontend localnodes    
 
 bind *:443 ssl crt /home/ssl/kccb-new.pem
    mode http
    option httplog
    capture request header X-Forwarded-For len 20
        capture request header authorization len 80
        capture request header X-Client-Auth len 80
        capture request header User-Agent len 400
        capture request header Host len 150        
        capture request header Accept-Language len 10       
    
    
    log-format "%{+Q}o\client_address=\%{+Q}[capture.req.hdr(0)],client_port=\%cp,server_address=\%si,server_port=\%sp,status=\%ST"
    http-response add-header Access-Control-Allow-Origin *
    http-response add-header X-Forwarded-For %{+Q}[capture.req.hdr(0)]
    rspadd Access-Control-Expose-Headers:\ *
    rspadd Access-Control-Allow-Headers:\ *
    rspadd Access-Control-Allow-Methods:\ GET,\ HEAD,\ OPTIONS,\ POST,\ PUT  if { capture.req.hdr(0) -m found }
    rspadd Access-Control-Allow-Credentials:\ true  if { capture.req.hdr(0) -m found }
    rspadd Access-Control-Allow-Headers:\ Origin,\ Accept,\ X-Requested-With,\ Content-Type,\ Access-Control-Request-Method,\ Access-Control-Request-Headers,\ Authorization  if { capture.req.hdr(0) -m found }

    acl url_kccb_mb path_beg /mb/    
    use_backend kccb_backend if url_kccb_mb
    acl url_sys_ha path_beg /sys/ha/
    use_backend sys_ha if url_sys_ha


backend kccb_backend
    balance roundrobin
    option forwardfor
    
#    http-request set-header X-Client-IP %[src]
    http-request set-header X-Forwarded-For %{+Q}[capture.req.hdr(0)]
    http-request replace-header ^([^\ :]*)\ /mb/(.*) \1\ /mbkccbxt/\2
    server kccb_mb 10.0.101.100:5000 check #maxconn 10000

backend sys_ha
    balance roundrobin
    http-request replace-header ^([^\ :]*)\ /sys/ha/(.*) \1\ /\2
    server sys-ha 127.0.0.1:8936 check

启动错误信息

第一次启动报错:

parsing [/etc/haproxy/haproxy.cfg:95] : The 'rspadd' directive is not supported anymore since HAProxy 2.>
 parsing [/etc/haproxy/haproxy.cfg:96] : The 'rspadd' directive is not supported anymore since HAProxy 2.>
 parsing [/etc/haproxy/haproxy.cfg:97] : The 'rspadd' directive is not supported anymore since HAProxy 2.>
 parsing [/etc/haproxy/haproxy.cfg:98] : The 'rspadd' directive is not supported anymore since HAProxy 2.>
 parsing [/etc/haproxy/haproxy.cfg:99] : The 'rspadd' directive is not supported anymore since HAProxy 2.>

替换reqrep为http-request replace-header后,再次启动报错:

parsing [/etc/haproxy/haproxy.cfg:95] : error detected in frontend 'xyzabc' while parsing 'http-r>

修复方案

HAProxy 2.x版本移除了rspadd指令,统一使用http-response add-header或http-response set-header处理响应头;同时修正http-request replace-header的转义语法,去掉不必要的转义字符:

修改后的关键配置片段

  1. 替换所有rspadd指令(在frontend xyzabc中):
http-response add-header Access-Control-Allow-Origin *
http-response add-header X-Forwarded-For %{+Q}[capture.req.hdr(0)]
http-response add-header Access-Control-Expose-Headers *
http-response add-header Access-Control-Allow-Headers *
http-response add-header Access-Control-Allow-Methods "GET, HEAD, OPTIONS, POST, PUT" if { capture.req.hdr(0) -m found }
http-response add-header Access-Control-Allow-Credentials true if { capture.req.hdr(0) -m found }
http-response add-header Access-Control-Allow-Headers "Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers, Authorization" if { capture.req.hdr(0) -m found }
  1. 修正http-request replace-header语法:
# backend kccb_backend中
http-request replace-header ^([^\ :]*) /mb/(.*) \1 /mbkccbxt/\2

# backend sys_ha中
http-request replace-header ^([^\ :]*) /sys/ha/(.*) \1 /\2

完整修改后的配置文件

global
   
    log         127.0.0.1 local2
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     5000
    user        haproxy
    group       haproxy
    daemon

tune.maxrewrite 4096
tune.http.maxhdr 202
    
    #tune.ssl.default-dh-param 2048
    tune.ssl.default-dh-param 2048  
    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats
    # utilize system-wide crypto-policies
    ssl-default-bind-ciphers AES256-SHA
    #ssl-default-bind-ciphers PROFILE=SYSTEM
    #ssl-default-server-ciphers PROFILE=SYSTEM

defaults
    mode                    http
    log                     global
    option                  httplog
    option                  forwardfor
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 10
    timeout http-request    40s
    timeout queue           1m
    timeout connect         40s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 60s
    timeout check           50s
    maxconn                 5000

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
#frontend  main *:5000

frontend xyzabc 
#frontend localnodes    
 
 bind *:443 ssl crt /home/ssl/kccb-new.pem
    mode http
    option httplog
    capture request header X-Forwarded-For len 20
        capture request header authorization len 80
        capture request header X-Client-Auth len 80
        capture request header User-Agent len 400
        capture request header Host len 150        
        capture request header Accept-Language len 10       
    
    
    log-format "%{+Q}o\client_address=\%{+Q}[capture.req.hdr(0)],client_port=\%cp,server_address=\%si,server_port=\%sp,status=\%ST"
    http-response add-header Access-Control-Allow-Origin *
    http-response add-header X-Forwarded-For %{+Q}[capture.req.hdr(0)]
    http-response add-header Access-Control-Expose-Headers *
    http-response add-header Access-Control-Allow-Headers *
    http-response add-header Access-Control-Allow-Methods "GET, HEAD, OPTIONS, POST, PUT" if { capture.req.hdr(0) -m found }
    http-response add-header Access-Control-Allow-Credentials true if { capture.req.hdr(0) -m found }
    http-response add-header Access-Control-Allow-Headers "Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers, Authorization" if { capture.req.hdr(0) -m found }

    acl url_kccb_mb path_beg /mb/    
    use_backend kccb_backend if url_kccb_mb
    acl url_sys_ha path_beg /sys/ha/
    use_backend sys_ha if url_sys_ha


backend kccb_backend
    balance roundrobin
    option forwardfor
    
#    http-request set-header X-Client-IP %[src]
    http-request set-header X-Forwarded-For %{+Q}[capture.req.hdr(0)]
    http-request replace-header ^([^\ :]*) /mb/(.*) \1 /mbkccbxt/\2
    server kccb_mb 10.0.101.100:5000 check #maxconn 10000

backend sys_ha
    balance roundrobin
    http-request replace-header ^([^\ :]*) /sys/ha/(.*) \1 /\2
    server sys-ha 127.0.0.1:8936 check

验证与启动

修改完成后先执行配置合法性检查:

haproxy -f /etc/haproxy/haproxy.cfg -c

若输出Configuration file is valid,再启动服务:

systemctl start haproxy

内容的提问来源于stack exchange,提问作者user2338456

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 09:32:04