0

I have two servers behind pfsense haproxy and I need to make sure users land on the same server based on a part of the url.

http://mydomain/<location>/..../....

In my case the is always the first slash after the domain, so all users with the same should end up on the same server.

Any suggestions?

h3li0s
  • 113
  • 4

1 Answers1

1

There's no native way to do this, but with a little bit of tinkering it can be made to work.

You'll need to extract the URL path component that you want in the Frontend, and place it in a throwaway header, which will then be used by your Backend to select the server.

Your frontend would look sort of like this:

frontend fe_pfsense
  ...
  http-request set-header X-Location-Path %[capture.req.uri]
  http-request replace-header X-Location-Path ([^/]+)/.* \1
  use_backend be_pfsense
  ...

First we extract the URI and place it in a throwaway header called X-Location-Path.
Then we use a regex to find and capture the first path component, and overwrite it to the same header.

And your backend would look sort of like this:

backend be_pfsense
  ...
  balance hdr(X-Location-Path)
  ...

We use the hdr() balance algorithm to balance based on the path we extracted in the Frontend.

Optionally, I think you could then drop the header before it goes out to the servers with either of the following lines, but you'd want to test this to make sure the order of events works as expected.

http-request del-header X-Location-Path

reqidel ^X-Location-Path:.*
GregL
  • 9,030
  • 2
  • 24
  • 35