Syntax
In HAProxy Enterprise, variables are used to store and manipulate values that can be used in various parts of the configuration file. Overall, variables provide a powerful mechanism for configuring and customizing HAProxy Enterprise's behavior based on runtime conditions and other factors
Temporary variables
You can use http-request set-var
and http-response set-var
for variables that live only for a given request, response, or client session.
frontend example
# Define a custom variable named 'my_string' and set its value to 'some string value'
http-request set-var(txn.my_string) str("some string value")
# Use the custom variable in the response header
http-response add-header X-My-String %[var(txn.my_string)]
In addition to setting variables explicitly, HAProxy Enterprise also provides a number of built-in fetches that are automatically set based on various aspects of the client request or server response. These include variables like src
, which contains the client's source IP address, and path
, which contains the path portion of the requested URL. More information can be found in the section Fetching samples.
Scoping
All variables are scoped, meaning they can only be accessed within the same scope. When you set a variable, you prefix its names with a scope. The scope can be any of the following:
proc: variable is available during all phases
sess: variable is available during a client's entire TCP session
txn: variable is available during an entire HTTP request-response transaction
req: variable is available during the HTTP request phase only
res: variable is available during the HTTP response phase only
Use the set-var
directives to set a variable:
http-request set-var
http-response set-var
http-response-after-response set-var
In the following example, the variable mypath
is scoped to the transaction, which makes it available during both the request and response phases:
frontend www bind :80 http-request set-var(txn.mypath) path
Unset a variable
Use one of the following directives to unset a variable:
http-request unset-var
http-response unset-var
http-response-after-response unset-var
Next up
Environment variables