我试图在每个域上加载并重定向多个路径映射。基本上检查域,如果域匹配,请使用此域路径映射(而不是主机头)。
# redirect
http-request redirect location %[path,lower,map(/etc/haproxy/maps/foo.textmap)] code 301 if { hdr(host) -i foo.com }
#foo.textmap
path https://bar.com/path
当我像这样卷曲时。看起来HAProxy正在返回301,但位置似乎丢失了
[centos@ip-10-121-111-57 ~]$ curl -ILvs --resolve foo.com:80:127.0.0.1 http://foo.com/path
* Added foo.com:80:127.0.0.1 to DNS cache
* About to connect() to foo.com port 80 (#0)
* Trying 127.0.0.1...
* Connected to foo.com (127.0.0.1) port 80 (#0)
> HEAD /path HTTP/1.1
> User-Agent: curl/7.29.0
> Host: foo.com
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
HTTP/1.1 301 Moved Permanently
< Content-length: 0
Content-length: 0
< Location:
Location:
<
* Connection #0 to host foo.com left intact
我在这里错过了什么吗?似乎应该在位置标题中返回地图中路径中的位置。 @Michael-sqlbot,您似乎是HAProxy世界中最有知识的人。有什么建议么?谢谢。
答案 0 :(得分:1)
您从路径的开头省略了/
,因此它不匹配。即,将文本映射更改为以下内容:
#foo.textmap
/path https://bar.com/path
结果:
> curl -I foo.com/path
HTTP/1.1 301 Moved Permanently
Content-length: 0
Location: https://bar.com/path
对于路径与地图中的某些内容不匹配的情况,您可能想要执行其他操作,而不是在301中发送空白位置。例如,这是一种修改,如果路径未匹配则重定向到默认网址找到:
# redirect
http-request redirect location %[path,lower,map(/etc/haproxy/maps/foo.textmap,https://bar.com/default)] code 301 if { hdr(host) -i foo.com }
否则,如果您只想在存在匹配项的情况下进行重定向,则您将编写一个更复杂的acl来匹配域,并且该域是否存在于地图中。
http-request redirect location %[path,lower,map(/etc/haproxy/maps/foo.textmap)] code 301 if { hdr(host) -i foo.com } { path,lower,map_str(/etc/haproxy/maps/foo.textmap) -m found }