haproxy - redirect specific domain/path to other domain - haproxy

i need redirect request from specific domain/path to other domain with same path, eg.
domain.com/foo/everything --> example.com/foo/everything
I think need use url_reg but I can't understand how to handle the redirect, maybe it could be such a thing?
acl redirect-foo url_reg -i ^domain.com\/foo\/*
http-request redirect code 301 location http://example.com/foo/ if redirect-foo
but obviously it doesn't work, thank anyone who can help me

You can use the following snipplet
acl host_match hdr(host) -i domain.com
acl path_match path_beg -i /foo/
http-request redirect code 301 location http://example.com/%[capture.req.uri] if host_match path_match
The acl's are described in the Documentation and in following Blog post.
Using ACLs to form conditions
Introduction to HAProxy ACLs
Documentation for capture.req.uri

Related

Haproxy Too Many Redirects

I want to route trafic outside of Turkey to another domain, I write acl into haproxy so in Turkey it works as planned but outside of Turkey traffic redirects but shows too many redirects. What I am missing in this configuration. My traffic comes from Cloudflare
acl acl_tr req.hdr(CF-IPCountry) eq "TR"
acl test_hname hdr(host) -m end lbtest.test.com
acl test_de hdr(host) -m end lbde.test.com
use_backend test_backends if acl_tr test_hname
redirect code 302 prefix https://lbde.test.com unless acl_tr
use_backend test_backends if test_de
If someone outside of Turkey accesses your site, they might get redirected once to https://lbde.test.com from your redirect rule. Assuming this hostname points to the same HAProxy instance, it will run into the very same redirect rule again, resulting in the same redirect to be sent.
To resolve this, you might want to leverage the test_de ACL there to not perform the redirect if the user is already requesting the lbde.test.com host:
redirect code 302 prefix https://lbde.test.com unless acl_tr || test_de
Finally, please make sure that you understand the order of rule evaluation in HAProxy. Although you have written your redirect rule after your first use_backend rule, it will still be evaluated before any use_backend rules.

Backend routing based on cookie name substring

Example cookie name in request:
wordpress_logged_in_8df6736080e8...
I want to create an haproxy acl based on when the cookie name begins with wordpress_logged_in and then route the logged in users based on that acl to separate backend.
acl url_admin path_beg -i /wp-admin /wp-login.php
acl url_admin hdr_sub(cookie) wordpress_logged_in
This config is working for me, as it matches the whole cookie header and some URLs. Without the first ACL it's not working.
You can try to use cook_beg for the acl
acl cookie_backend cook_beg(wordpress_logged_in) -m found
...
use_backend cookie_backend if cookie_backend
...
default_backend default_backend
This Blog post explains the haproy acl Introduction to HAProxy ACLs
In the doc can you find more details Using ACLs to form conditions

Haproxy - 301/302 redirect URL1 to URL2 with all pathes

I'm a little bit lost atm. I try to implement a redirect within a complex HAproxy configuration. The goal is simple:
user uses a subdomain -> 123.domain.com
user will be pointed to api.domain.com but thinks it's still on 123.domain.com
user should be able to use pathes like 123.domain.com/123?123 but still get results from api.domain.com/123?123 but thinks it's gets result from 123.domain.com/123?123.
I'm totally unsure how to implement that without taking the rist of taking down production traffic.
What i would do:
creating ACL rule in SSL frontend to point to the api backend when 123.domain.de is used.
redirect prefix http://123.domain.com code 301 if { hdr(host) -i api.domain.com }
Not usre if that would work.
creating redirection rule pointing simply to another api url:
redirect location https://www.mysites/v2/pages 302 if { hdr(host) -i api.domain.com }
It's hard to implement this wihtout taking a risk of an outage. Is there something who could know the answer?
First I would recommend that you have a development and/or staging environment configured to test any changes before you make them.
To do a 301 redirect of all traffic coming from 123.domain.com to api.domain.com you can use the following
http-request redirect prefix http://api.domain.com code 301 if { hdr(host) -i 123.domain.com }
If you wanted HAProxy to connect to the api.domain.com backend on the users behalf and mask the hostname itself you would add a backend for api.domain.com and then create a use_backend rule. Keep in mind that with the below there are no 301/302 redirects performed, instead HAProxy makes the connection on behalf of the client.
Something like this would work:
use_backend api.domain.com if { hdr(host) -i 123.domain.com }
Then within api.domain.com backend you can update the Host header.
backend api.domain.com
http-request set-header Host api.domain.com
server api1 api.domain.com:80 check

HAProxy Redirects Of Domain & Domain w/Subdomains

I’m trying to redirect a domain and 1 other subdomain of that domain, but it’s not working (2 other redirects work to https, but they’re both different domains from each other and from this domain in question) and I haven’t been able to find an example that matches my configuration.
Here’s a snippet of the code in the order the entries appear, but let me know if you think you need to see more of the code (a lot of front ends and backends exist).
acl is_test1.domain.com hdr_dom(host) -i test1.domain.com
acl is_domain.com hdr_dom(host) -i domain.com
redirect location https://test1.domainTwo.com/ if is_domain.com
redirect location https://test1.domainTwo.com/path/ if is_test1.domain.com
Also, how should www.domain.com be handled with this configuration? Do I need another acl for it?
Thanks!
Not sure if it's the most efficient setup, but the suggestion below is similar to what I have and it works.
acl is_test1.domain.com hdr(host) -i test1.domain.com
acl is_domain.com hdr(host) -i domain.com www.domain.com
The redirects should work from there as the acl's will match appropriately now. From my understanding of HAProxy, hdr_dom(host) will match any dot-delimited part of the domain (taking the more exact match) and hdr(host) will match if the domains are identical.

Haproxy redirect www to non-www

I'm currently using Haproxy to balance several express.js nodes. I know that it's possible to redirect using express.js, but I was hoping to do so with Haproxy.
I was wondering how I can do a permanent redirect from www.mysite.com to mysite.com?
redirect prefix http://example.com code 301 if { hdr(host) -i www.example.com }
Please see the documentation of the redirect prefix rule for more information.
If you are using a newer version of HAProxy, i.e. at least 1.6, you can use a more generic syntax which allows to redirect any host, not just explicitly named
http-request redirect prefix http://%[hdr(host),regsub(^www\.,,i)] code 301 if { hdr_beg(host) -i www. }
Here, we are using the regsub filter to dynamically generate the correct hostname without the www. prefix.
In case you want to perform a redirect the other way around, i.e. to add a www if there is none already, the rule becomes simpler:
http-request redirect prefix http://www.%[hdr(host)] code 301 unless { hdr_beg(host) -i www. }