Search code examples
haproxy

HaProxy Adding Header on url path match


I am in the process of migrating from F5 big IP to Haproxy. I have this rule in F5 :

when HTTP_REQUEST {

  if { [HTTP::uri] starts_with "/download" } {
        HTTP::header insert "X-ENDPOINT-NAME" static
  } else {
        HTTP::header insert "X-ENDPOINT-NAME" www
  }

And I have translated it to Haproxy like this :

http-request add-header X-ENDPOINT-NAME static if { path_dir /download } || { path_dir /download/ }
http-request add-header X-ENDPOINT-NAME www if !{ path_dir /download } || !{ path_dir /download/ }

When I check on the varnish server behind the HaProxy server I never match the static case. For example I try https://host.com/download/file.css

I use this tcpdump command to see the result on the varnish server : tcpdump -i eth0 -s 0 -A 'tcp[((tcp[12:1] & 0xf0) >> 2):4] = 0x47455420'

How can I match a uri starting with /download

I am using : HAProxy version 2.5.5-384c5c5

I have try a lot of options, sperate acl call by the http-request line, path_beg, path. It never match the case of static.

On the varnish server I always have X-ENDPOINT-NAME: www


Solution

  • What we do in HAProxy is defining a different backend for static resources.

        # get images and other static data from varnish
        use_backend varnish if { path_beg /static/ }
    

    In this case all paths that start with /static are directed to varnish.