HAProxy and URL Rewriting Configuration

Ianthe the Duke of Nukem picture Ianthe the Duke of Nukem · Nov 20, 2011 · Viewed 22.6k times · Source

I would like to ask how HAProxy can help in routing requests depending on parts of the URL.

To give you an overview of my setup, I have the HAProxy machine and the two backends:

  • IIS website (main site)
  • Wordpress blog on NGINX (a subsite)

The use-case:

I'm expecting to route requests depending on the URL:

  • www.website.com/lang/index.aspx -> main site
  • www.website.com/lang/blog/articlexx -> blog subsite

The blog access URL is "/server/blog/lang/articlexx" so I have to rewrite the original client request to that format--which is basically switching "blog" and "lang".

From how I understood the configuration documentation and some posts on the net, I could use reqrep/reqirep to change the request HTTP headers before it gets passed to a backend. And if that's right, then this configuration should work:

frontend vFrontLiner
    bind            x.x.x.x:x
    mode            http
    option          httpclose
    default_backend iis_website

    # the switch: x/lang/blog -? x/blog/lang
    reqirep ^/(.*)/(blog)/(.*) /if\2/\1/\3

    acl blog path_beg -i /lang/blog/

    use_backend blog_website if blog


backend blog_website
    mode    http
    option  httpclose
    cookie  xxblogxx insert indirect nocache
    server  BLOG1 x.x.x.x:80 cookie s1 check inter 5s rise 2 fall 3
    server  BLOG2 x.x.x.x:80 cookie s2 check inter 5s rise 2 fall 3 backup

The problem: The requests being received by the blog_website backend is still the original URL "x/lang/blog".

I might have missed something on the regex part but my main concern is whether my understanding correct or not to use the reqirep in the first place. I would appreciate any help.

Thanks very much.

Answer

w00t picture w00t · Jan 31, 2012

Your regex is wrong, you're assuming the server is in the request path. To match the request paths in the headers use a regex like this one:

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

you can use reqirep as well but that is only useful if your servers actually serve /BLog/lAnG/ as well.