This is a quick guide on how to validate or “test” HAProxy’s haproxy.cfg file for errors.
Typically, changes to HAProxy require a restart. However, this restart will fail if there is an error in the configuration file. If this occurs, the load balancer will be unable to serve traffic.
This can be a tense situation, as you either need to rollback to the original file or fix the errors that are preventing it from restarting.
As a result, you should always test your HAProxy configuration file before you restart it.
Validating the HAProxy file without restarting.
To validate the HAProxy configuration file without restarting, you can use the following command.
sudo /usr/local/sbin/haproxy -c -V -f /etc/haproxy/haproxy.cfg
You may need to modify the command above to match the file paths on your server. However, in most cases, this should work straight out of the box. Especially if you are using Ubuntu / Debian.
A quick drill-down of this command and what it does:
- /usr/local/sbin/haproxy: This is the path to where HAProxy is installed.
- -c: The “-c” flag tells HAProxy to test the configuration file. Furthermore, it also tells HAProxy not to bind the file in question.
- -V: This flag stands for “verbose”. This instructs HAProxy to print any errors or warnings to the command line so that we can easily view them.
- -f: The “-f” flag tells HAProxy that we want to load a configuration file.
- Finally, “/etc/haproxy/haproxy.cfg” is the location of our config file. Obviously, if your file is in a different location, you will need to change this file path.
Once you run this command, you will either be told that file is valid or that errors / warnings are present. Note that “warnings” and “notices” won’t prevent your load balancer from restarting. However, they should still be fixed in order to prevent other issues.
See related: Test nginx file.