KokLiang.LIM
KokLiang.LIM

Reputation: 25

HAProxy path to host/path/

Please bear with me as I am not a coder by nature.

This is what I trying to achieve using HAproxy but after hours of checking, I am unable to make it works somehow.

From

  1. domain.com/alpha
  2. domain.com/beta

To

  1. domain.com/alpha will point to backend1/path/index.cgi
  2. domain.com/beta will point to backend2/path/index.cgi

I have tried, multiple ways but come to no avail, I did read about rewrite/redirect but somehow it makes me very confuse. eg "reqrep"

by using the alpha.domain.com points to backend1/path works as expected but I need the path inline because of certificate limitation.

Thank you in advance and if possible explain abit how it works and whats the correct terms (example: rewrite, redirect) so that I can have clue on that and I will advance from there.

Upvotes: 2

Views: 18924

Answers (1)

JamesStewy
JamesStewy

Reputation: 1078

This is what I was able to come up with:

frontend HTTP
    mode http
    bind *:80

    acl alpha url_beg /alpha
    acl beta url_beg /beta
    use_backend backend_alpha if alpha 
    use_backend backend_beta if beta

backend backend_alpha
    reqrep ^([^\ ]*\ /)alpha[/]?(.*)     \1path/index.cgi
    server server_alpha localhost:8080

backend backend_beta
    reqrep ^([^\ ]*\ /)beta[/]?(.*)     \1path/index.cgi
    server server_beta localhost:8081

Obviously you would then replace localhost:8080 and localhost:8081 with the correct locations for your case.

Explanation

First, in the frontend named HTTP there are two ACLs (Access Control Lists) which test what is in the beginning of the URL (hence the keyword url_beg). The result of these rules are if the url begins with /alpha then the variable called alpha is set to true and then the same for beta.

Next in the frontend, there are two use_backend commands which will direct the request to backend_alpha if the variable alpha is set to true and the same for backend_beta if beta is set to true.

As a result the frontend does the work of taking the url and deciding which server to use.

The two backends (backend_alpha and backend_beta) are almost identical except for the text alpha and beta and the location of the respective servers. The first command in the backend is the reqrep command which you pointed out. What the reqrep command does is takes a url, searches for a particular part using Regular Expression and then replaces it with something else. In this case we have this url:

http://example.com/alpha

In the first part of the reqrep command:

  • ^([^\ ]*\ /) takes http://example.com/ and stores it in a variable called \1
  • alpha then matches with the alpha in the given url
  • [/]?(.*) takes everything after alpha and stores it in a variable called \2 (in this case \2 would equal nothing as there is nothing after alpha in the url)

Then the second part of the reqrep command says take the contents of \1 (http://example.com/) and add path/index.cgi to the end and make that the new url to send to the server.

As a result for both alpha and beta urls the resulting url sent to the server is http://example.com/path/index.cgi.

Finally the server command sends the request of to the appropriate server.

I would like to point out I am not an expert on the complicated Regular Expression part (I find it a bit confusing as well) but hopefully someone else who knows a bit more can explain it in more detail or correct me if I am wrong.

I hope that helps :)

Upvotes: 18

Related Questions