Core concepts
Fetch methods
A fetch method is a function that returns information about the current request, current response, connection, or internal state of the load balancer. You use a fetch by referencing its function name with optional arguments. One example is the path
fetch, which returns the requested URL path. In the following example, we check whether path
equals /api/bad
and, if it does, deny the request:
haproxy
frontend wwwbind :80http-request deny if { path /api/bad/ }
haproxy
frontend wwwbind :80http-request deny if { path /api/bad/ }
The path
fetch does not take any arguments. An example of a fetch that does is req.hdr
, which takes the name of a request header to collect. Below, we use req.hdr
to get the User-Agent header on an http-request capture
line. The http-request capture
directive adds custom data to the log.
haproxy
frontend wwwbind :80http-request capture req.hdr(user-agent) len 30
haproxy
frontend wwwbind :80http-request capture req.hdr(user-agent) len 30
In the next example, we demonstrate two other fetches. The http-request set-var
directive sets a variable named txn.http_version
to the value returned by the fetch req.ver
, which returns the HTTP version of the request. We also use the http-response add-header
directive to add a new response header named Via
. Its value holds the HTTP version that we stored in a variable plus the server’s hostname, which we get by using the hostname
fetch, which takes no arguments.
haproxy
frontend wwwbind :80http-request set-var(txn.http_version) req.verhttp-response add-header Via "%[var(txn.http_version)] %[hostname]"
haproxy
frontend wwwbind :80http-request set-var(txn.http_version) req.verhttp-response add-header Via "%[var(txn.http_version)] %[hostname]"
This example also demonstrates that some fetches are collected during the request phase of a session and are not available during the response phase. However, you can store the fetch value in a variable by using http-request set-var
to make it available throughout the entire transaction. In fact, var(<variable name>)
is itself a fetch.
See also Jump to heading
Do you have any suggestions on how we can improve the content of this page?