Can I split a large HAProxy config file into multiple smaller files?

Stumbled on this answer where the author created scripts to imitate nginx disable enable sites functionality. In the haproxy init.d startup he uses script loop to build the haproxy -f commands concatenation.

/etc/init.d/haproxy:

EXTRAOPTS=`for FILE in \`find /etc/haproxy/sites-enabled -type l | sort
-n\`; do CONFIGS="$CONFIGS -f $FILE"; done; echo $CONFIGS`

haensite script:

#!/bin/bash

if [[ $EUID -ne 0 ]]; then
  echo "You must be a root user" 2>&1
  exit 1
fi

if [ $# -lt 1 ]; then
  echo "Invalid number of arguments"
  exit 1
fi

echo "Enabling $1..."

cd /etc/haproxy/sites-enabled
ln -s ../sites-available/$1 ./

echo "To activate the new configuration, you need to run:"
echo "  /etc/init.d/haproxy restart"

hadissite script:

#!/bin/bash

if [[ $EUID -ne 0 ]]; then
  echo "You must be a root user" 2>&1
  exit 1
fi

if [ $# -lt 1 ]; then
  echo "Invalid number of arguments"
  exit 1
fi

echo "Disabling $1..."

rm -f /etc/haproxy/sites-enabled/$1

echo "To activate the new configuration, you need to run:"
echo "  /etc/init.d/haproxy restart"

Configuration files can't be linked together from a configuration directive.

However HAProxy can load multiple configuration files from its command line, using the -f switch multiple times:

haproxy -f conf/http-defaults -f conf/http-listeners -f conf/tcp-defaults -f conf/tcp-listeners 

If you want to be flexible with the amount of config files you can even specify a directory like this: -f /etc/haproxy. The files will then be used in their lexical order, newer files overriding older files. See the mailing list for an example, if provides links to the documentation. This information can be found in the management guide, not the regular docs.


This was a solution building off of @stephenmurdoch's answer which involved the use of multiple -f <conf file> arguments to the haproxy executable.

Using the stock CentOS 6.x RPM's included /etc/init.d/haproxy script you can amend it like so:

start() {
    $exec -c -q -f $cfgfile $OPTIONS
    if [ $? -ne 0 ]; then
        echo "Errors in configuration file, check with $prog check."
        return 1
    fi

    echo -n $"Starting $prog: "
    # start it up here, usually something like "daemon $exec"
    #daemon $exec -D -f $cfgfile -f /etc/haproxy/haproxy_ds.cfg -f /etc/haproxy/haproxy_es.cfg -f /etc/haproxy/haproxy_stats.cfg -p $pidfile $OPTIONS
    daemon $exec -D -f $cfgfile $(for i in /etc/haproxy/haproxy_*.cfg;do echo -n "-f $i ";done) -p $pidfile $OPTIONS
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

With the above in place you can then create files such as haproxy_<X>.cfg and haproxy_<Y>.cfg using whatever names you want. The above for loop will include these files in an augmented daemon haproxy ... line if these files are present, otherwise the stock haproxy.cfg file will be used solely.

Within the haproxy_<...>.cfg files you need to make sure that your global and defaults are defined in the "toplevel" haproxy.cfg file. The rest of the files simply need to have frontend/backends and nothing more.


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/

Tags:

Haproxy