我正在寻找与下面这个后端代码块等价的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(主机)作为http响应statement语句中的值。
http-response set-header X-Target %[req.hdr(Host)]
server web-servers site.%[req.hdr(Host),regsub(^www.,,)]:80 checkHA-代理版本2.1.4-273103-54 2020/05/07 - https://haproxy.org/
当我尝试haproxy -c -f haproxy.test时,我得到了这个错误
根@pm-prod-haproxy05 haproxy# haproxy -c -f haproxy.test警报259/180932 (16116):解析haproxy.test:40:‘HTTP-response set-header’:这里可能无法可靠地使用样例提取,因为它需要‘header’,这在这里不可用。警报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发布于 2020-05-06 22:37:08
关于头操作的
正如警告消息所述,您不能在响应中使用请求头。您应该替换下面的一行。
错线
http-response set-header X-Target %[req.hdr(Host)]右线
http-request set-header X-Target %[req.hdr(Host)]后端服务器不应删除此标头。如果不想向后端服务器发送‘X’主机头,那么可以使用会话变量将主机标头从请求保存到响应阶段。
http-request set-var(txn.my_host) req.hdr(host),lower
http-response set-header X-Target %[var(txn.my_host)]在文档中,set-var和set-头指令很好地解释了。
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-请求do-解析的一些部分,以获得这个答案。
您需要在配置中添加一个节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不会被用来扫描网络,或者最坏的情况不会自己循环.
https://stackoverflow.com/questions/57997530
复制相似问题