looks like you are trying to do the web site’s job at the proxy level. What is supposed to be done by the Web application should be done by that application and not compensated for by a proxy.
Also, the example you gave is again pretty basic. Either give the full extent of the problem so we can work it with you, or instead, we can only provide you with the same basic solutions.
From here, I guess your logic is wrong / over complicated. You should re-design your logic and I expect a simpler way will emerge from that.
Or as another solution, you could cascade multiple processing stages to compensate for your messy logic :
HAproxy receives the request on a first socket and routes the request based on a first criteria like domain. Requests for Domain1 are sent to local port 1, Domain2 is sent to port 2, Domain 3 is sent to port 3, each of them with the required header for the corresponding domain (Header Dx).
On the FrontEnd listening on Port 1, you don’t need to test for Domain1 anymore because you know it is set. It has been sorted already. So all you need now is to work based on path. Path A sent to port A, path B sent to port B, … again adding the proper Header Px.
On that third FrontEnd, do whatever you need and keep going as long as you need.
At the end of the maze, all your backends may go to the same place or to different places, as you need. But each query will have been modified according to the criteria from its corresponding HAProxy path routing.
Have fun reviewing your logic,