Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HAProxy and URL Rewriting Configuration

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.

like image 579
Ianthe the Duke of Nukem Avatar asked Nov 20 '11 15:11

Ianthe the Duke of Nukem


People also ask

What is HAProxy ACL?

Access Control Lists, or ACLs, in HAProxy allow you to test various conditions and perform a given action based on those tests.

What is front end and back end in HAProxy?

The frontend is the node by which HAProxy listens for connections. Backend nodes are those by which HAProxy can forward requests. A third node type, the stats node, can be used to monitor the load balancer and the other two nodes.


1 Answers

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.

like image 164
w00t Avatar answered Sep 20 '22 15:09

w00t