Validate HAProxy Configuration File

Run the following to validate a configuration file that is used by HAProxy.

haproxy -c -f /etc/haproxy/haproxy.cfg

The -c flag is used for turning on configuration check mode, the -f flag allows a configuration file to be passed to the program.

For a successful configuration file you should see the text "Configuration file is valid" being returned. The program will also return an exit code of 0.

In full:

$ haproxy -c -f /etc/haproxy/haproxy.cfg
Configuration file is valid
$ echo $?
0

If there is a problem you will see information about the problem.

For example, if we attempted to validate the following configuration that contains a type on the word "serverr" in the backend section:

defaults
    timeout connect 5000
    timeout client  50000
    timeout server  50000
frontend haproxy_as_api_gateway
    bind 127.0.0.1:80
    default_backend load_balancer
backend load_balancer
    serverr server1 127.0.0.1:8080
    server server2 127.0.0.1:8081

This will produce an incorrect validation and will print out any problems it found.

$ haproxy -c -f /etc/haproxy/haproxy.cfg
[NOTICE]   (26857) : haproxy version is 2.6.12-1+deb12u2
[NOTICE]   (26857) : path to executable is /usr/sbin/haproxy
[ALERT]    (26857) : config : parsing [/etc/haproxy/haproxy.cfg:9] : unknown keyword 'serverr' in 'backend' section; did you mean 'server' maybe ?
[ALERT]    (26857) : config : Error(s) found in configuration file : /etc/haproxy/haproxy.cfg
[ALERT]    (26857) : config : Fatal errors found in configuration.

The exit state of the command will also be 1, indicating that an error happened in the validation.

$ echo $?
1

Add new comment

The content of this field is kept private and will not be shown publicly.