HAProxy Enterprise Documentation 2.7r1

Environment variables

HAProxy Enterprise supports the use of environment variables to customize its configuration or behavior. These are variables that are defined outside of HAProxy Enterprise, in the operating system or container environment, and are passed to HAProxy Enterprise when it starts. Environment variables can be used to customize the behavior of HAProxy Enterprise and make it easier to manage complex configurations.

By using environment variables to define configuration values, you can reduce the amount of hard-coded values in your configuration files and make it easier to reuse configuration values across multiple instances of HAProxy Enterprise.

HAProxy Enterprise can read environment variables at startup.

Set environment variables in the service file

One way to add environment variables is to append them to the Environment line in the file /lib/systemd/system/hapee-2.7-lb.service. Then HAProxy Enterprise will have access to them when it starts up as a service.

We append an environment variable named PROXYNAME to the Environment line:

Environment="CONFIG=/etc/hapee-2.7/hapee-lb.cfg" "PIDFILE=/run/hapee-2.7-lb.pid" "PROXYNAME=example"

You can then read the environment variable from the HAProxy Enterprise configuration by prefixing it with a dollar sign and surrounding it with quotes.

We retrieve the value of the PROXYNAME environment variable to use in the Via HTTP request header's value:

frontend www
  mode http
  bind :80

  # The Via header will contain the HTTP version and the proxy name
  http-request add-header Via "%[req.ver] $PROXYNAME"

Set an environment variable with sentenv and presetenv

You can also set environment variables from the HAProxy Enterprise configuration. The setenv directive in the global section sets a new or overrides an existing environment variable.

We use sentenv in the global section to override the PROXYNAME variable:

  setenv PROXYNAME example2

frontend www
  bind :80

  # Uses the overridden value
  http-request add-header Via "%[req.ver] $PROXYNAME"

Alternatively, use the presetenv directive to set an environment variable only if it has not already been set. If that variable is already set, it will not be overridden.

We use presetenv in the global section to set the PROXYNAME variable only if it has not already been set.

  presetenv PROXYNAME example2

frontend www
  mode http
  bind :80

  # Uses the value from the environment if it was set
  http-request add-header Via "%[req.ver] $PROXYNAME"

Read an environment variable

To read the value of an environment variable, precede it with a dollar sign. Optionally, enclose it in curly braces. In most cases, you must surround the variable with quotes.

In this example, we read several variables in the configuration.

  setenv IP    
  setenv PORT            8080
  setenv USER            foo
  setenv PASS            bar
  setenv DEFAULT_BACKEND be_app
  setenv LOG_ADDRESS
  setenv LOG_LEVEL       notice

  # Set syslog logging
  log "$LOG_ADDRESS" local0 "$LOG_LEVEL"

userlist credentials
  # Set a Basic auth username and password
  user "$USER" insecure-password "$PASS"

frontend fe_main
  # Set the IP address and port using variables
  bind "$IP":"$PORT"

  # enforce Basic authentication
  http-request auth unless { http_auth(credentials) }

  default_backend "$DEFAULT_BACKEND"

In ACL expressions, which expect a fetch method, use the env method to read the variable.

We read the environment variable MAINTENANCE_ON when deciding whether to show a down for maintenance web page.

  # Use 1 for true and 0 for false

frontend fe_main
  bind :80

  # Show a 'down for maintenance' page
  http-request return  status 503  content-type text/html  file /etc/hapee-2.7/maintenance.html if { env(MAINTENANCE_ON) -m bool }

  default_backend be_app

See also

Next up

Reference Manual