【问题标题】:HAProxy Maps based on paths, if host matchHAProxy Maps 基于路径,如果主机匹配
【发布时间】:2019-05-09 20:12:36
【问题描述】:

我正在尝试在每个域的基础上加载和重定向多个路径映射。基本上检查域,如果域匹配,使用这个域路径图(不是主机头)。

# 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 世界中知识最渊博的。有什么建议么?谢谢。

【问题讨论】:

    标签: haproxy


    【解决方案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 中发送一个空位置。例如,如果路径不匹配,这里有一个修改将重定向到默认 URL发现:

    # 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 }
    

    【讨论】:

    • 就是这样,缺少斜线。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2012-08-28
    • 2014-02-12
    • 1970-01-01
    • 1970-01-01
    • 2014-09-07
    • 1970-01-01
    • 1970-01-01
    • 2013-01-19
    相关资源
    最近更新 更多