Skip to content

Commit 41d906d

Browse files
einval22wtarreau
authored andcommitted
DOC: configuration: update "Environment variables" chapter
There are some variables, which are set by HAProxy process (HAPROXY_*). Some of them are handy to check or to redefine in the configuration, in order to create conditional blocks and make the configuration more flexible. But it wasn't clear in the documentation, which variables are really safe and usefull to redefine and which ones could be only read via "show env" output. Latest changes in master-worker architecture makes the existed description even more confusing. So let's sort all HAPROXY_* variables to four categories and let's also mark explicitly, which ones are set in which process, when haproxy is started in master-worker mode. In addition, update examples in chapter "2.4. Conditional blocks". This might bring more ideas for users how HAPROXY_* variables could be used in the conditional blocks.
1 parent d6ccd17 commit 41d906d

File tree

1 file changed

+102
-7
lines changed

1 file changed

+102
-7
lines changed

doc/configuration.txt

Lines changed: 102 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,77 @@ existing variables, not empty ones.
935935
user "$HAPROXY_USER"
936936

937937
Some variables are defined by HAProxy, they can be used in the configuration
938-
file, or could be inherited by a program (See 3.7. Programs):
938+
file, or could be inherited by a program (See 3.7. Programs). These variables
939+
are listed in the matrix below, and they are classified among four categories:
940+
941+
* usable: the variable is accessible from the configuration, either to be
942+
resolved as-is, or used within conditional blocks or predicates to enable
943+
or disable this some configuration fragments, as described in section 2.4
944+
"Conditional blocks".
945+
946+
* modifiable: the variable can be redefined or unset in the configuration via
947+
"setenv"/"unsetenv" keywords.
948+
949+
* listed: the variable is listed in CLI's "show env" command output,
950+
described in section 9.3 "Unix Sockets commands" of the management guide.
951+
952+
* exported: variable is exported to launch programs in a modified environment
953+
(See section 3.7 "Programs"). Note that this does not apply to external
954+
checks which have their own rules regarding exported variables.
955+
956+
There also two subcategories "master" and "worker", respectively marked 'M' and
957+
'W' in the table below, showing the differences between the two processes when
958+
HAProxy is launched in master-worker mode.
959+
960+
* master: the variable is set and accessible from the master process. So, it
961+
will appear in the master CLI's "show env" output and it can be used in
962+
conditional blocks or directives to enable some special settings for the
963+
master (see examples in section 2.4 "Conditional blocks").
964+
965+
* worker: the variable is set and accessible from the worker process. It will
966+
appear in the worker CLI's "show env" (or the master CLI's "@1 show env")
967+
and it may as well condition some worker process parameters (see examples
968+
from section 2.4 "Conditional blocks").
969+
970+
In standalone mode (without "-W" option nor the "master-worker" keyword) the
971+
process behaves like a worker, except for variables "HAPROXY_MASTER_CLI" and
972+
"HAPROXY_MWORKER" which are not defined.
973+
974+
Some variables are marked as not usable and not modifiable:
975+
976+
* HAPROXY_CFGFILES
977+
* HAPROXY_MWORKER
978+
* HAPROXY_CLI
979+
* HAPROXY_MASTER_CLI
980+
* HAPROXY_LOCALPEER
981+
982+
Their values are undefined during configuration parsing, they are set later
983+
during the initialization. So, it's recommended not to use these variables
984+
within conditional blocks and not to reference them in the global section's
985+
"setenv"/"resetenv"/"unsetenv" keywords.
986+
987+
The table below summaries the status of each variable for the different working
988+
modes:
989+
990+
+--------------------------+----------+---------+------------+-----------+
991+
| variable | exported | usable | modifiable | listed |
992+
| | +---------+------------+-----------+
993+
| | | M | W | M | W | M | W |
994+
+--------------------------+----------+----+----+------+-----+-----+-----+
995+
| HAPROXY_STARTUP_VERSION | X | X | X | | | X | X |
996+
| HAPROXY_BRANCH | X | X | X | | | X | X |
997+
| HAPROXY_CFGFILES | X | | | | | X | X |
998+
| HAPROXY_MWORKER | X | | | | | X | X |
999+
| HAPROXY_CLI | | | | | | | X |
1000+
| HAPROXY_MASTER_CLI | | | | | | X | |
1001+
| HAPROXY_LOCALPEER | | | | | | | X |
1002+
| HAPROXY_HTTP_LOG_FMT | | | X | | X | | |
1003+
| HAPROXY_HTTP_CLF_LOG_FMT | | | X | | X | | |
1004+
| HAPROXY_HTTPS_LOG_FMT | | | X | | X | | |
1005+
| HAPROXY_TCP_LOG_FMT | | | X | | X | | |
1006+
+--------------------------+----------+----+----+------+-----+-----+-----+
1007+
1008+
The variables in question are the following:
9391009

9401010
* HAPROXY_LOCALPEER: defined at the startup of the process which contains the
9411011
name of the local peer. (See "-L" in the management guide.)
@@ -968,8 +1038,8 @@ file, or could be inherited by a program (See 3.7. Programs):
9681038

9691039
* HAPROXY_MWORKER: In master-worker mode, this variable is set to 1.
9701040

971-
* HAPROXY_CLI: configured listeners addresses of the stats socket for every
972-
processes, separated by semicolons.
1041+
* HAPROXY_CLI: configured listeners addresses of the stats socket of every
1042+
processe, these addresses are separated by semicolons.
9731043

9741044
* HAPROXY_MASTER_CLI: In master-worker mode, listeners addresses of the master
9751045
CLI, separated by semicolons.
@@ -1006,8 +1076,6 @@ This way it is possible to emit information to help locate a rule in variables,
10061076
logs, error statuses, health checks, header values, or even to use line numbers
10071077
to name some config objects like servers for example.
10081078

1009-
See also "external-check command" for other variables.
1010-
10111079

10121080
2.4. Conditional blocks
10131081
-----------------------
@@ -1139,12 +1207,41 @@ The list of currently supported predicates is the following:
11391207

11401208
Example:
11411209

1210+
# 1. HAPROXY_MWORKER variable is set automatically by HAProxy in master and
1211+
# in worker process environments (see HAProxy variables matrix from
1212+
# 2.3. Environment variables). Its presence enables an additional listener.
1213+
1214+
global
1215+
master-worker
1216+
11421217
.if defined(HAPROXY_MWORKER)
11431218
listen mwcli_px
11441219
bind :1111
11451220
...
11461221
.endif
11471222

1223+
# 2. HAPROXY_BRANCH is set automatically by HAProxy in master and in worker
1224+
# process environments (see HAProxy variables matrix from 2.3. Environment
1225+
# variables). We check HAPROXY_BRANCH value and conditionally enable
1226+
# mworker-max-reloads parameter.
1227+
1228+
global
1229+
master-worker
1230+
1231+
.if streq("$HAPROXY_BRANCH",3.1)
1232+
mworker-max-reloads 5
1233+
.endif
1234+
1235+
# 3. Some arbitrary environment variables are set by user in the global
1236+
# section. If HAProxy is started in master-worker mode, they are presented in
1237+
# master and in worker process environments. We check values of these
1238+
# variables and conditionally enable ports 80 and 443. Environment variables
1239+
# checks can be mixed with features and version checks.
1240+
1241+
global
1242+
setenv WITH_SSL yes
1243+
unsetenv SSL_ONLY
1244+
11481245
.if strneq("$SSL_ONLY",yes)
11491246
bind :80
11501247
.endif
@@ -11291,8 +11388,6 @@ external-check command <command>
1129111388
PATH The PATH environment variable used when executing
1129211389
the command may be set using "external-check path".
1129311390

11294-
See also "2.3. Environment variables" for other variables.
11295-
1129611391
If the command executed and exits with a zero status then the check is
1129711392
considered to have passed, otherwise the check is considered to have
1129811393
failed.

0 commit comments

Comments
 (0)