Choose backend with path_beg

Hi,

I need something like this:

test.example.com/blog → will be served by 192.168.10.1:8080
test.example.com/shop → will be served by 192.168.10.2:8080

So I got this:

        acl blog_pathbeg            path_beg        -i /blog
        acl shop_pathbeg        path_beg        -i /shop
        acl test_example_hdr                 hdr(host)       -i test.example.com

        use_backend blog_backend            if      blog_pathbeg   test_example_hdr 
        use_backend shop_backend            if      shop_pathbeg  test_example_hdr 

 backend blog_backend 
                mode http
                balance roundrobin
                server blog_server 192.168.10.1:8080  check inter 5000 rise 2 fall 2

 backend shop_backend 
                mode http
                balance roundrobin
                server shop_server 192.168.10.2:8080  check inter 5000 rise 2 fall 2

But… The thing is, that my blog and shop apps are directly in 192.168.10.1:8080 and 192.168.10.2:8080 (not in 192.168.10.1:8080/blog and 192.168.10.2:8080/shop).

Can I fix this with haproxy? Or do I need to move my apps to be served by 192.168.10.1:8080/blog and 192.168.10.2:8080/shop ?

You need to tell your applications that they are not served with a root path “/”.

You can rewrite the URL in the HTTP requests with haproxy. However you cannot rewrite all the links in the HTML payload that point to the wrong URL.

That is why there are no complete workarounds for this in haproxy.

1 Like

as @lukastribus said, there are two problems. first, it is no problem to rewrite urls in haproxy

backend shop_backend 
http-request replace-path /shop    /
server shop_server 192.168.10.2:8080  check inter 5000 rise 2 fall 2

should do the trick. but there is one problem, that your app will serve html pages with links like

http://192.168.10.2:8080/products

and not

http://test.example.com/shop/products

so you need all your links written with /shop prefix. there is no built-in function to modify response body. you could do this with LUA in haproxy.

if your shop/blog software allows programming it would work, if you check some http-header and then write links based on http-header. for example set some http header in haproxy, your shop/blog can check this header and if it is set then write links like /shop/products or /blog/history and if the header is missing write links like /products or /history

you don’t need mode http and balance roundrobin in backend, you can configure this in global conf

1 Like