HAProxy community

Subdirectory frontend to root backend

Hello

I’m looking for some help with figuring out the best way to handle routing URLs with subdirectories to applications hosted on the root of a webserver.

Say I have URL domainx.com/path1, I want users to be sent to an application on server web2 that is running an application in the root of tomcat.

If I use the below config it will send users to that tomcat server but will direct users to 192.168.0.51:8080/path1 not 192.168.0.51:8080 If I move the application to 192.168.0.51:8080/path1 this will work. But what if the application can’t be renamed or relocated. What is the best way to accomplish this?

Thanks for any help in advance!

 	acl url_domainx.com hdr(host) -i domainx.com
	acl url_domainx.com-path1 path_beg -i /path1
	
	use_backend domainx.com if url_domainx.com-path1 url_domainx.com
	use_backend domainx.com if url_domainx.com
 
backend domainx.com
        server web1 192.168.0.50:8080 check
		
backend domainx.com-path1
        server web2 192.168.0.51:8080 check

have you explored “http-request set-uri” ? or below might even work

acl url_domainx.com hdr(host) -i domainx.com
acl url_domainx.com-path1 path_beg -i /path1

use_backend domainx.com-path1 if url_domainx.com-path1 url_domainx.com
use_backend domainx.com if url_domainx.com

backend domainx.com
http-request set-uri / (not sure if this required , but please try)
server web1 192.168.0.51:8080 check

backend domainx.com-path1
server web2 192.168.0.51:8080 check

Hi senattcs

Thanks for your reply. Yes, I was playing with that a little bit with an example like yours. that will work to get you to the main page of a site but then if you click to any link it will strip that as well to root.
e.g.
going to domianx.com/path1 takes you correctly to the main page

clicking a link that you want to take you to domianx.com/path1/page2 will take you to domianx.com/page2.

clicking a link that you want to take you to domianx.com/path1/page2 will take you to domianx.com/page2.

I don’t think so. Because we are not setting URI in bucket “domainx.com-path1”, so it should use same URI whatever came in the frontend request.

“I don’t think so. Because we are not setting URI in bucket “domainx.com-path1”, so it should use same URI whatever came in the frontend request.”

Hmm, I setup the above config to test it just now and that’s what I’m seeing. I’m not sure why it’s happening but that’s what I’m experiencing.

Can you change the order of your acls and try please?

Hi Senattcs

Should the order of the acls have any affect? I thought that would only apply to the use_backend statements that are calling them?

I can’t test the changing the order just yet as I had to take down that test environment but will spin up another one to change to order for testing shortly.