haproxy - HAProxy 中的动态服务器名称和标头
问题描述
我正在寻找与下面这个后端代码块等效的对 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 2020/05/07 - https://haproxy.org/
当我尝试 haproxy -c -f haproxy.test 时出现此错误
[root@pm-prod-haproxy05 haproxy]# haproxy -c -f haproxy.test [ALERT] 259/180932 (16116) : parsing [haproxy.test:40]: 'http-response set-header': sample fetch may在这里不能可靠地使用,因为它需要此处不可用的“HTTP 请求标头”。[警报] 259/180932 (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
解决方案
关于标题操作
正如 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.io/haproxy-dconv/1.8/configuration.html#4-http-request
关于服务器操作
此行无法工作,因为 haproxy 尝试在启动时解析目标服务器。
server web-servers site.%[req.hdr(Host),regsub(^www.,,)]:80 check
在较新版本的 haproxy 中。像2.1一样,可以动态解析和设置目标主机吗?
http://cbonte.github.io/haproxy-dconv/2.1/configuration.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 不会被用于扫描网络,或者最坏的情况不会自循环......
推荐阅读
- java - 如何从日志文件生成 java 堆使用图?
- javascript - 2 可以同时过滤的过滤器切换案例
- flutter - 如何在 Flutter 中删除图像和文本之间的空格
- automation - 新电子邮件到达时未触发 Microsoft 流程
- kotlin - Kotlin 中的 Swift 枚举等价物
- php - 无法向 Laravel 端点发出 HTTP 请求
- android - Retrofit2 缓存混淆
- python - 循环拉取python中列的前2个子字符串
- excel - Excel 格式日期从 DATEVALUE 到 TEXT 与公式
- javascript - 如果数据集为空,Chart.js 饼图将显示“无数据”