HAProxy Enterprise Documentation 2.7r1


A variable holds a value from a fetch so that you can use that value later in your configuration. Typically, you need them when you want to store a value from a fetch that is only available when the load balancer receives a request, but not when returning the response. For example, although the response phase of the session typically does not have access to data available during the request phase, you can store request-phase data in a variable to make it available later.

The following does not work and results in the error when reloading:

frontend www
   bind :80
   http-response add-header X-Path %[path]
   # error: sample fetch <path> may not be reliably used here

However, this works because the variable named txn.mypath stores the path value:

frontend www
   bind :80
   http-request set-var(txn.mypath) path
   http-response add-header X-Path %[var(txn.mypath)]
   # success!

To retrieve the value of a variable, you use the var fetch method.

In the next snippet, the req.hdr fetch does not return a value during the response phase and so the if condition always evaluates to false. Therefore, this configuration does not work properly:

frontend www
   bind :80
   http-response add-header X-Message str("My message") if { req.hdr(user-agent) -i -m sub firefox }
   # failure: header will not be added

However, the following works because the variable txn.useragent stores the header's value so that it is available during the response phase:

frontend www
   bind :80
   http-request set-var(txn.useragent) req.hdr(user-agent)
   http-response add-header X-Message str("My message") if { var(txn.useragent) -i -m sub firefox }
   # success! header is added

Next up