如何在HAProxy中仅重定向选定子域?

如何在HAProxy中仅重定向选定子域?,proxy,reverse-proxy,haproxy,http-proxy,Proxy,Reverse Proxy,Haproxy,Http Proxy,我有几个域名,比如abc.blah.com,xyz.blah.com,2a.blah.com,3b.blah.com等等。在我的haproxy.cfg文件中,我只想处理其中的几个域,而将其余域留给它们指定的目的地 例如: 将abc和xyz重定向到不同的目标 abc.blah.com==10.1.1.11 xyz.blah.com==10.1.1.12 但不是2a.blah.com或3b.blah.com或*.blah.com。让他们直接到达实际目的地 这意味着2a.blah.com应该转到2a.

我有几个域名,比如
abc.blah.com
xyz.blah.com
2a.blah.com
3b.blah.com
等等。在我的haproxy.cfg文件中,我只想处理其中的几个域,而将其余域留给它们指定的目的地

例如:

abc
xyz
重定向到不同的目标

abc.blah.com==10.1.1.11

xyz.blah.com==10.1.1.12

但不是
2a.blah.com
3b.blah.com
*.blah.com
。让他们直接到达实际目的地

这意味着
2a.blah.com
应该转到
2a.blah.com

这是haproxy.cfg

global
    log /dev/log    local0
    log /dev/log    local1 notice
    chroot /var/lib/haproxy
    user haproxy
    group haproxy
    daemon

defaults
    log     global
    mode    http
    option  httplog
    option  dontlognull
    contimeout 5000
    clitimeout 50000
    srvtimeout 50000
    errorfile 400 /etc/haproxy/errors/400.http
    errorfile 403 /etc/haproxy/errors/403.http
    errorfile 408 /etc/haproxy/errors/408.http
    errorfile 500 /etc/haproxy/errors/500.http
    errorfile 502 /etc/haproxy/errors/502.http
    errorfile 503 /etc/haproxy/errors/503.http
    errorfile 504 /etc/haproxy/errors/504.http



frontend http-in
    bind 0.0.0.0:80
    acl xyz hdr(host) -i xyz.blah.com
    acl abc hdr(host) -i abc.blah.com
    acl default hdr_end(host) -i .blah.com

    ## figure out which one to use
    use_backend xyz_cluster if xyz
    use_backend abc_cluster if abc
    use_backend direct_forward if default

# send it to xyz.blah.com 
backend xyz_cluster
    option forwardfor
    server node1 10.1.1.12:8080

# send it to "abc.blah.com"
backend abc_cluster
    option forwardfor
    server node1 10.1.1.11:8080

# handle 2a.blah.com
# handle 3b.blah.com
# handle *.blah.com
# basically forwarding to the source itself
backend direct_forward
    option httpclose
    option http_proxy


listen  stats   :9000
    mode  http
    stats enable
    stats hide-version
    stats realm Haproxy\ Statistics
    stats uri  /stats
    stats auth test:test123
通过此配置,对
xyz
abc
的请求被正确路由

curl -x 10.148.240.78:80  http://xyz.blah.com
很好

但是对
2a.blah.com
的请求抛出了503

curl -x 10.148.240.78:80  http://2a.blah.com
<html><body><h1>503 Service Unavailable</h1>
No server is available to handle this request.
</body></html>
curl-x10.148.240.78:80http://2a.blah.com
503服务不可用
没有可用于处理此请求的服务器。
haproxy日志显示以下内容:

Mar 30 16:48:17 ubuntu-ha-proxy-3289 haproxy[9105]: 10.254.184.246:54533 [30/Mar/2017:16:48:17.793] http-in direct_forward/<NOSRV> 2/-1/-1/-1/2 503 213 - - SC-- 0/0/0/0/0 0/0 "GET http://2a.blah.com HTTP/1.1"
Mar 30 16:48:17 ubuntu-ha-proxy-3289 haproxy[9105]:10.254.184.246:54533[30/Mar/2017:16:48:17.793]http直接转发/2/-1/-1/-2 503 213--SC--0/0/0/0/0“GEThttp://2a.blah.com HTTP/1.1“
基本上,
直接转发
后端需要配置为将请求传递到源域本身。但尚不清楚它是如何在haproxy中实现的

当没有匹配“使用后端”规则时,使用
default\u backend

使用在前端和后端之间切换内容时 “use_backend”关键字,指示将使用哪个后端通常很有用 当没有匹配的规则时使用。它通常是动态后端 将捕获所有未确定的请求


来源:

您可以通过配置后端来实现这一点。通常,如果请求到达haproxy,则haproxy是预期的目的地,您需要配置请求实际用于的主机。这些对
*.blah.com
的请求首先将如何到达haproxy?这也是一个值得注意的问题,您所做的并不是正确地称为“重定向”。您描述的是将请求转发到后端。重定向web请求是指返回几个30x HTTP响应代码中的一个,以告知浏览器向不同的URL发出不同的请求。
frontend http-in
    bind 0.0.0.0:80
    acl xyz hdr(host) -i xyz.blah.com
    acl abc hdr(host) -i abc.blah.com

    ## figure out which one to use
    use_backend xyz_cluster if xyz
    use_backend abc_cluster if abc

    default_backend direct_forward