HAProxy Enterprise Documentation 2.2r1

Overview

A fetch 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 parameters. One example is the path fetch, which returns the requested URL path. In the following example, we check whether path equals /api/weather and, if it does, deny the request:

frontend www
   bind :80
   http-request deny unless { path /api/weather/ }

The path fetch does not take any parameters. An example of a fetch that does is req.hdr, which takes the name of a request header to collect.

Here, 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.

frontend www
   bind :80
   http-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 is set to 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 parameters.

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.


Next up

Syntax