How to make HAProxy redirect traffic via a certain sub-proxy depending on URL?

maximalian@sopuli.xyz to Programming@programming.dev – 10 points –

I have a server (S-1) with HAProxy and a number of residential proxies (PR-s) as login-passwords-port. There're multiple users who will connect to the internet via S-1.

I want to have HAProxy to forward incomming traffic of the users via a random proxy amoung PR-s with 2 conditions:

  1. only when there's certain, pre-defined keyword in the URL, traffic must be routed via a proxy.
  2. In all other cases, it must go to a requested resource directly as is, without a proxy

How would I implement this?


(1) client -> HaProxy -> if keyword --> sub-proxy (random) -> website

(2) client -> HaProxy -> if no keyword --> website

6

Maybe have two cases:
For the (matched keyword) case, something like this: https://serverfault.com/questions/729232/reg-exp-for-url-in-haproxy

For the (random routing) case, something like: https://www.haproxy.com/blog/haproxy-configuration-basics-load-balance-your-servers

I am a little confused on your question, though. It sounds like you maybe want 3 cases? Can you try wording it differently?

  1. client -> HaProxy -> if keyword --> sub-proxy (random) -> website
  2. client -> HaProxy -> if no keyword --> website

Cool, I think that first link will work for you. Then you can just 'redirect' for the no-keyword case: https://www.haproxy.com/documentation/haproxy-configuration-tutorials/http-redirects/

Remember, SSL/HTTPS does encrypt the URL path, so if the final website requires HTTPS, your proxy will need to have its certificate trusted by your clients

There're 2 conditions in my question. In the links the solutions described are partial, though. A part of a part. That is, "how to identify a keyword", "how to redirect if ...".

But how to actually put them all together? For instance, how to merge "if a keyword then redirect via a pre-specified, random sub-proxy"? How to them add into all that "otherwise, use no proxy"?

In your HAProxy config (like in this example), it's checked from top to bottom. So your top-most frontend case should be the one that checks for the special keyword case. Then, if it doesn't match the keyword, HAProxy will continue going down the list until it finds a frontend that does match. So your second frontend should match for everything, because if it doesn't find a match, HAProxy shows an error