5

I have almost 200 lines of ACL configurations in haprox.cfg and also contains 150 backends. To remove this configuration complexity, I want to bundle this configuration in separate files and will import those files in haprox.cfg. Is this possible in haproxy?

user364875
  • 61
  • 1
  • 1
  • 3

3 Answers3

10

As far as I know HAproxy does not have anything similar to apache's Include & IncludeOptional directives.

There is no native support for multiple configuration files other than starting HAproxy with repeated -f <config-file> command line switches. see this thread.

You can script something though to merge multiple subsections into a larger file similar to this approach although I would probably go the route and modify the init script to append additional configuration files automatically (untested):

# Load additional configuration snippets from /etc/haproxy.d/*.cfg
OPTIONS=""
for file in /etc/haproxy.d/*.cfg ; do test -f $file && OPTIONS="$OPTIONS -f $file" ; done

start() {
  /usr/sbin/$BASENAME -c -q -f /etc/$BASENAME/$BASENAME.cfg $OPTIONS
  if [ $? -ne 0 ]; then
    echo "Errors found in configuration file, check it with '$BASENAME check'."
    return 1
  fi

  echo -n "Starting $BASENAME: "
  daemon /usr/sbin/$BASENAME -D -f /etc/$BASENAME/$BASENAME.cfg $OPTIONS -p /var/run/$BASENAME.pid
  RETVAL=$?
  echo
  [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$BASENAME
  return $RETVAL
}
HBruijn
  • 77,029
  • 24
  • 135
  • 201
4

I see the version 1.7.5-2 2017/05/17 has the following help:

       -f <configuration file|dir>
              Specify  configuration  file or directory path. If the argument is a directory the files (and only files) it contains
              are added in lexical order (using LC_COLLATE=C) ; only non hidden files with ".cfg" extension are added.

So in my case editing /etc/default/haproxy so that it contains

CONFIG="/etc/haproxy/"

enables me to put more .cfg files in /etc/haproxy and all is read and configured.

Glorfindel
  • 1,213
  • 4
  • 15
  • 22
dosmanak
  • 161
  • 1
  • 6
0

You can follow this simple step.

  1. Insert one line script (cat /etc/$BASENAME/conf.d/*.cfg > $CFG) in /etc/init.d/haproxy
    Here is position where you must insert line
    CFG=/etc/$BASENAME/$BASENAME.cfg cat /etc/$BASENAME/conf.d/*.cfg > $CFG [ -f $CFG ] || exit 1
  2. Reload daemon config with systemctl daemon-reload
  3. Make directory mkdir /etc/haproxy/conf.d
  4. Move default haproxy.cfg to conf.d as global.cfg mv /etc/haproxy/haproxy.cfg /etc/haproxy/conf.d/global.cfg
  5. Create your other .cfg file in conf.d directory
  6. Just restart your haproxy service systemctl restart haproxy
  7. NOTE: /etc/haproxy/haproxy.cfg will be automaticly created from all files in conf.d/
dek.tiram
  • 91
  • 3