Nambi
Nambi

Reputation: 2717

How to redirect only select subdomains in HAProxy?

I have several domains such as abc.blah.com, xyz.blah.com, 2a.blah.com, 3b.blah.com and many more. In my haproxy.cfg file I would like to handle only a few of the domains and leave the rest to go their intended destination.

For example:

Redirect abc and xyz to different destinations

abc.blah.com == 10.1.1.11

xyz.blah.com == 10.1.1.12

But not 2a.blah.com or 3b.blah.com or *.blah.com. Let them be directed to the actual destination.

That means 2a.blah.com should go to 2a.blah.com

here is the 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

With this configuration, the requests to xyz and abc are routed correctly.

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

Works fine

But requests to 2a.blah.com are throwing 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>

The haproxy log shows the following,

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"

Basically the direct_forward backend needs to be configured to pass the requests to the source domains itself. But it is unclear how it is achieved in haproxy.

Upvotes: 1

Views: 5905

Answers (1)

Tan Hong Tat
Tan Hong Tat

Reputation: 6864

Use default_backend <backend> when no "use_backend" rule has been matched.

When doing content-switching between frontend and backends using the "use_backend" keyword, it is often useful to indicate which backend will be used when no rule has matched. It generally is the dynamic backend which will catch all undetermined requests.

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

Source: https://cbonte.github.io/haproxy-dconv/1.7/configuration.html#default_backend

Upvotes: 3

Related Questions