Score:1

HaProxy - Abillity to redirect subdomains and the base domain to the same backend

my flag

I need some help with the HaProxy setup.

Currently, I have the following config:

frontend fe_main_http
  bind :80
  reqadd X-Forwarded-Proto:\ http
  use_backend %[req.hdr(Host),lower]

frontend fe_main_https
    bind 0.0.0.0:443 ssl crt /etc/haproxy/certs/ no-sslv3 no-tls-tickets
    reqadd X-Forwarded-Proto:\ https
    use_backend %[req.hdr(Host),lower]


backend test1.domain.com
    redirect scheme https if !{ ssl_fc }
    server s1 1.2.3.4:80 check

...

This means, that any domain name will be redirected to the appropriately named backend. (For example: example.com to backend example.com)

The thing is - I want it to be set up for the subdomains of those domains as well. Basically speaking, any subdomain of the base domain (e.g. test.example.com or *.example.com) and the base domain itself (e.g. example.com) should point to the same backend (e.g. backend example.com).

I've heard that it can be done with regex; however, I had no luck with setting this thing up. :(

Maybe someone has faced a similar thing and can help me with that?

Thanks in advance!

Score:1
cz flag

You don't really need a regex (and remember they are expensive) but you do need to specify the domain name.

Consider this:

        use_backend example.com if { req.hdr(host) -i example.com }

Now match for all subdomains with -m end:

        use_backend example.com if { req.hdr(host) -i -m end .example.com }
Rodion Grinberg avatar
my flag
Yes, but the thing is - I want to use that with multiple domains, and creating more "use_backend" will clutter the config. Is there a cleaner solution?
Michael Hampton avatar
cz flag
@RodionGrinberg And creating more `backend`s won't clutter the config?
Rodion Grinberg avatar
my flag
Well, it will; but not as much as additionally configuring frontend and backend side. I am adding the new backend using bash script, so, if possible, can the current config be modified?
Michael Hampton avatar
cz flag
Well then you can add `use_backend` by script too!
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.