4

I am very new to HAProxy. I spent a few hours trying to figure out how to do it but could not get any leads. My requirement is this:

If end point of request is /special then I need to check URL_PARAM.

For example: localhost/special?id=10

Based on ID, I need to route it to one of the 3 servers. If id <=3 server1, if id > 3 and id <=6 server2 else server3.

If end point is not /special round robin between all 3 servers.

How do I achieve this 2 level balancing?

1 Answer 1

4

You can use urlp and urlp_val to extract the id. Then, use acl to match the integer:

acl is_special path_beg /special acl small_id urlp_val(id) le 3 acl medium_id urlp_val(id) 4:6 acl high_id urlp_val(id) gt 6 use_backend bck1 if is_special small_id use_backend bck2 if is_special medium_id use_backend bck3 if is_special high_id default_backend bck_all 

Then, create 3 backends: one for each case.

Edit:

If you want to use regex on the query param, use urlp_reg:

acl small_id urlp_reg(id) ^[0-3] acl medium_id urlp_reg(id) ^[4-6] acl high_id urlp_reg(id) ^[7-9] 

Also check stick, depending on what you're trying to do.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot! But what if integer ID is really big. If it can only be considered as string. Apologies for not making it clear in the example. Can I use some sort of regex range match on it?
Sorry, I just realized from my data sample that the IDs are from basically UIDs.
Perfect! Thanks a lot!
Does the comparison support 64 bit numbers? I have a range like "456304162255302657" to "456316501914435584" and so on and this doesn't seem to be able to compare them. This does work for smaller numbers though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.