HAProxy community

Backend as subdir?


I had such great success with my first question that I thought I would ask my next one. I’m looking for whether this is possible and what the general approach might be. I’d like to figure it out myself but I think I need a direction.

The question is … can I use a subdirectory, as in my first question, to select the backend server AND have that server unaware of the subdirectory?

In my first question the backend knew how to handle X-Script-Name: which was passed to it. In the new case the backend is a webcam and doesn’t seem to honor that header. I use reqrep to remove the subdir on the inbound request, is there a way to put it back in on the response? Or is there a better way than I’ve thought of?

Any gentle pointers will be appreciated.


Yes, using the reqrep directive in the backend should achieve that exactly.

[quote=“ctgreybeard, post:1, topic:691”]I use reqrep to remove the subdir on the inbound request, is there a way to put it back in on the response? Or is there a better way than I’ve thought of?

I don’t understand, the response in HTTP does not contain the URI, so there is nothing to “put it back in”…


Well, I think the problem is that the content contains links to build the web page, scripts, images, etc. and they are not based including the subdirectory name. Then when the browser requests them they are not recognized in the frontend and fail because the wrong system gets the request.

There has to be a way to do it, I just haven’t found it yet.


I just had a thought. What if I inject a cookie into the response then detect it in the frontend to select the backend?

The first request would come in with the subdirectory fine and get to the correct backend, that response would have the cookie inserted with rspadd in the backend. The frontend would be looking for that cookie and would select the correct backend if it is found, even though the subdirectory isn’t present in the request.



In the backend I use cookie HAPROXYSRV insert nocache indirect with the cookie server4 keyword on the server line.

Then in the frontend I have:

	acl subdir_req path_beg /subdir/
	acl subdir_req path /subdir
	acl subdir_req req.cook(HAPROXYSRV) -m str server4

	use_backend sudbdir_srv if subdir_req

backend subdir_srv
	reqrep ^([^\ :]*)\ /subdir/?(.*)  \1\ /\2
	cookie HAPROXYSRV insert nocache indirect
	server server4 cookie server4

I think I need to work on interference among the cookies but the concept is a good start I think. As I am the only one who might be using these subdirectory redirects I think I can handle the occasional anomaly.


This can work, but when you have to consult a different service/backend, you gonna have to clear the cookies in the browser, right?



In fact I’ve spent the afternoon redefining it to be hostname based. I had hoped to drive everything more easily without having to manage DNS on top of it but it didn’t seem like that was going to work out.

So I’ve still got the cookie thing in but will probably take it out and I’ve added HTTPS termination and redirection using lets-encrypt so I did get a benefit.