在HAProxy中动态的服务器名称和头。

问题描述 投票:0回答:1

我正在寻找相当于下面这个后端代码块的请求www.example.com 和 example.com。

http-response set-header X-Target  example.com
server web-servers  site.example.com:80 check

我把所有的请求都发送到www.example.com,但我想用haproxy把它们发送到site.example.com。example.com有几个不同的域名,所以我想有一个允许的域名列表,如果它们被允许,我想有一个像下面这样的后端代码块,我可以使用%[req.hdr(Host)]作为http-response X-Target语句的值。

http-response set-header X-Target  %[req.hdr(Host)]
server web-servers  site.%[req.hdr(Host),regsub(^www.,,)]:80 check

HA-Proxy 2.1.4-273103-54版本 20200507 - https:/haproxy.org。

当我尝试haproxy -c -f haproxy.test时,我得到这个错误。

[root@pm-prod-haproxy05 haproxy]# haproxy -c -f haproxy.test[ALERT] 259180932 (16116) : 解析 [haproxy.test:40]: 'http-response set-header': 样本获取可能无法可靠地在这里使用,因为它需要'HTTP请求头',而这里没有。 [ALERT] 259180932 (16116) : 在配置文件中发现错误:haproxy.test[root@pm-prod-haproxy05 haproxy]#。

我也试过这个。

http-request set-header X-Target  %[req.hdr(Host)]
http-request set-header X-Hostname %[req.hdr(Host),regsub(^www.,site.,)]
http-request web-server do-lookup(hdr(X-Hostname))
server web-servers  web-server:80 check

这是我的全部配置

global
log         127.0.0.1 local2 debug
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    daemon
    stats socket /var/lib/haproxy/stats

defaults
    mode                    http
    option                  httplog
    log                     global
    option                  dontlognull
    option                  http-server-close
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

frontend frontend-http

    bind *:80
    bind *:443

    acl redirect path_beg -i /rd
    use_backend backend-tracking if redirect

default_backend backend-default

backend backend-default
    option forwardfor
    http-response set-header X-Publishing-system website
    http-response set-header X-Target  %[req.hdr(Host)]
    server web-servers  site.%[req.hdr(Host),regsub(^www.,,)]:80 check

backend backend-tracking
    option forwardfor
    http-response set-header X-Publishing-system redirect
    http-request set-uri %[url,regsub(^/rd,/,)]
    server web-hp www.trackingplatform.com:80 check
haproxy
1个回答
1
投票

关于头的操作

正如ALERT信息中所说,你不能在响应中使用请求头。你应该替换下面的行。

错行

http-response set-header X-Target  %[req.hdr(Host)]

右线

http-request set-header X-Target  %[req.hdr(Host)]

后端服务器不应该删除这个头。如果你不想向后端服务器发送 "X-Target "主机头,那么你可以使用一个会话变量来保存从请求到响应阶段的主机头。

http-request set-var(txn.my_host) req.hdr(host),lower
http-response set-header X-Target %[var(txn.my_host)]

在文档中,set-var和set-header指令有很好的解释。http:/cbonte.github.iohaproxy-dconv1.8configuration.html#4-http-request。

关于服务器的操作

这一行无法工作,因为haproxy会在启动时尝试解析目标服务器。

server web-servers site.%[req.hdr(Host),regsub(^www.,,)]:80 check

在新版本的haproxy中,比如2.1,你可以动态地解析和设置目标主机。http:/cbonte.github.iohaproxy-dconv2.1configuration.html#4.2-http-request%20do-resolve。

我假设你想改变目标服务器的主机头,正确的虚拟服务器是使用。我的建议是解决你的问题,改变主机头,并将服务器名称设置为可解析地址。

backend backend-default
  option forwardfor

  http-response set-header X-Publishing-system website

  http-request set-header X-Target %[req.hdr(Host)]

  http-request replace-header Host ^www(.*) site.\1
  http-request set-header X-NewTarget %[req.hdr(Host),regsub(^www.,,)]

  server web-servers  site.example.com:80 check

这个后台配置只是语法检查。

关于动态后台服务器

server 应该是动态解决的.为了这个解决方案,至少需要HAProxy 2.0。

我在这里复制文档的一些部分 http-request do-resolve 为这个答案。

您需要添加一节 resolvers 到您的配置

resolvers mydns
  # use here your prefered DNS Servers
  nameserver local 127.0.0.53:53
  nameserver google 8.8.8.8:53
  timeout retry   1s
  hold valid 10s
  hold nx 3s
  hold other 3s
  hold obsolete 0s
  accepted_payload_size 8192

frontend frontend-http

  bind *:80
  bind *:443

  # define capture buffer for backend
  declare capture request len 60

  acl redirect path_beg -i /rd
  use_backend backend-tracking if redirect

  default_backend backend-default

# ... some more backends

backend backend-default
  option forwardfor

  http-response set-header X-Publishing-system website

  http-request set-header X-Target %[req.hdr(Host)]

  # replace www with site in host header
  http-request replace-header Host ^www(.*) site.\1

  # if necessary set X-NewTarget header
  http-request set-header X-NewTarget %[req.hdr(Host),regsub(^www.,,)]

  # do dynamic host resolving for dynamic 
  # server destination for 
  # the replaced Host Header above 
  http-request do-resolve(txn.myip,mydns,ipv4) hdr(Host),lower

  # print the resolved IP in the log
  http-request capture var(txn.myip) id 0

  # rule to prevent HAProxy from reconnecting to services
  # on the local network (forged DNS name used to scan the network)
  # add the IP Range for the destination host here
  http-request deny if { var(txn.myip) -m ip 127.0.0.0/8 10.0.0.0/8 }
  http-request set-dst var(txn.myip)

  server clear 0.0.0.0:0

请注意文件中的说明

注意:不要忘记设置 "保护 "规则,以确保HAProxy不会被用来扫描网络,或者最坏的情况下不会自我循环......。

© www.soinside.com 2019 - 2024. All rights reserved.