HAProxy and URL Rewriting Configuration

23,225

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.

Share:
23,225
Ianthe the Duke of Nukem
Author by

Ianthe the Duke of Nukem

LoL

Updated on July 09, 2022

Comments

  • Ianthe the Duke of Nukem
    Ianthe the Duke of Nukem almost 2 years

    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.

  • Tino
    Tino over 10 years
    The regexp shall be either in frontend with if blog appended in the above case, or in the blog_website backend. Also it should spell reqrep ^([^\ :]*)\ /lang/blog/(.*) \1\ /blog/lang/\2 to not match Headers (the difference is a :). And Cookies are not rewritten in case they have a path=, but this is difficult, anyway.
  • ThiamTeck
    ThiamTeck about 6 years
    hi @Tino, I am facing the problem of cookies path after URL rewrite, do you able to share some sample on how to deal with that ?
  • Tino
    Tino about 6 years
    @ThiamTeck I'd try something like http-response replace-value Set-Cookie ;\ path=/blog/lang ;\ path=/lang/blog see: haproxy.com/documentation/aloha/9-0/traffic-management/… (Note: I think this page misses some '\' in the replacements examples.) But above needs to be adapted to your needs, for example path may be just /blog - as I said, it is difficult. CAVEAT: I did not test it nor needed it ever and all here was done just by the docs, not by testing it with HaProxy.