real quick question regarding HAProxy reqrep. I am trying to rewrite/replace the request that gets sent to the backend.

I have the following example domain and URIs, both sharing the same domain name, but different backend web server pools.


I want web1 to go to backend webfarm1, and web2 to go to webfarm2. Currently this does happen. However I want to strip off the web1 or web2 URI when the request is sent to the backend.

Here is my haproxy.cfg

frontend webVIP_80
        mode http
        bind    :80
        #acl routing to backend
        acl web1_path path_beg /web1
        acl web2_path path_beg /web2

        #which backend
        use_backend webfarm1 if web1_path
        use_backend webfarm2 if web2_path
        default_backend webfarm1

backend webfarm1
        mode http
        reqrep ^([^\ ]*)\ /web1/(.*)     \1\ /\2
        balance roundrobin
        option httpchk HEAD /index HTTP/1.1\r\nHost:\ example.com
        server webtest1 weight 5 check slowstart 5000ms
        server webtest2 weight 5 check slowstart 5000ms
backend webfarm2
        mode http
        reqrep ^([^\ ]*)\ /web2/(.*)     \1\ /\2
        balance roundrobin
        option httpchk HEAD /index HTTP/1.1\r\nHost:\ example.com
        server webtest1-farm2 weight 5 check slowstart 5000ms
        server webtest2-farm2 weight 5 check slowstart 5000ms

If I go to http://domain/web1 or http://domain/web2 I see it in the error logs that the request on a server in each backend that the requst is for the resource /web1 or /web2 respectively. Therefore I believe there to be something wrong with my regular expression, even though I copied and pasted it from the Documentation. http://code.google.com/p/haproxy-docs/wiki/reqrep

Summary: I'm trying to route traffic based on URI, however I want HAProxy to strip the URI when it sends the request to the backend pool.

Thank you!


  • There's an edit and comment buttons now.
    – Chris S
    Nov 7, 2012 at 20:30
  • 1
    Jim, you're having a problem commenting because you're logged in with a different account than you used to create this question. Please try and keep your login account consistent so that this doesn't happen in the future. I've flagged a moderator to fix this.
    – MDMarra
    Nov 7, 2012 at 20:30

3 Answers 3


You have this:

reqrep ^([^\ ]*)\ /web1/(.*)     \1\ /\2

I think you want this:

reqrep ^([^\ ]*\ /)web1[/]?(.*)     \1\2

The difference being that the second one will work if the / after webN is omitted.

In answer to your comment below, going in to detail about how the expressions above work is more effort than I can give. However, maybe this will help.

Everything before /web1 is "capturing" everything that comes before web1 in the request string. So usually that would be GET or POST. The (.*) "captures" everything after web1, including nothing if there is nothing.

The next part (\1\2) says what to do with those captured parts. It says to form a string composed of \1 (the first captured part) and \2 (followed by the second captured part). Since web1 is never captured, it's not assembled in to the final output.

  • What does the \1\ \2\3 signify? Regarding the reqrep documentation what does the ^([^\ ]*)\ do? Here is the example given on the documentation page. # replace "/static/" with "/" at the beginning of any request path. reqrep ^([^\ ]*)\ /static/(.*) \1\ /\2
    – Jim
    Nov 7, 2012 at 20:40
  • After reading your comment, I realized that my proposed solution was slightly overly complicated. I simplified it.
    – longneck
    Nov 7, 2012 at 20:53
  • So what is the reqrep syntax? reqrep {string_2_search_thru} {search_string} {replace_string}? I am confused as to the \1\ \2\3?
    – Jim
    Nov 7, 2012 at 21:00
  • Is there a way to incorporate this reqrep to work if the user puts /web1 or /web1/ ?
    – Jim
    Nov 8, 2012 at 1:56
  • Did you try it?
    – longneck
    Nov 8, 2012 at 12:32

Since HAProxy version 2.1, there's no reqrep directive. So you can use following:

http-request replace-path /web1(.*) \1
  • 1
    This is more actual answer. Thanks!
    Apr 21 at 19:44

Put this on the frontend

reqirep  ^([^\ :]*)\ /(.*) \1\ /web1/\2

Good lesson learn to strip string: https://gist.github.com/PiBa-NL/8ad6c222354cbd7a5af5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.