79

Is there any way to validate the HAProxy haproxy.cfg file before restarting the HAProxy service? For example: There might be a small spelling/syntax error in a larger haproxy.cfg file. I searched through several forums, but was unable to find anything in relation to validating the haproxy.cfg files for syntax errors.

As of now, I use a trial and error basis on a developer machine before I upload the changes to a Production Server.

starball
  • 20,030
  • 7
  • 43
  • 238
Shawn
  • 2,679
  • 2
  • 15
  • 26
  • 2
    Are you using something like `service haproxy reload`? If you are, it should already work like this... it won't restart unless the config is valid, and the old process is unaffected. – Michael - sqlbot Sep 21 '16 at 20:55
  • I'm using the `service haproxy restart`. Perhaps I should try the `service haproxy reload` instead. I wanted to know if there was some way of passing/validating the `haproxy.cfg` before restarting it. – Shawn Sep 22 '16 at 10:51

2 Answers2

149

The official HaProxy configuration file check was buried in the help sections.

/usr/local/sbin/haproxy --help

There are two ways to check the haproxy.cfg syntax is to use..

One way is the /usr/local/sbin/haproxy -c -V -f /etc/haproxy/haproxy.cfg which validates the file syntax. The -c switch in the command represents the Check, while the others denote "Verbose" & "file".

Another way is to sudo service haproxy configtest

I hope this helps anyone looking to check the syntax of the haproxy.cfg file before restarting the service.

starball
  • 20,030
  • 7
  • 43
  • 238
Shawn
  • 2,679
  • 2
  • 15
  • 26
  • 16
    `sudo service haproxy configtest` is not a recognize option for me with the last haproxy – rogercampos Jun 15 '17 at 19:45
  • 12
    `/usr/sbin/haproxy -c -V -f /etc/haproxy/haproxy.cfg` works for me. Returns "Configuration file is valid" – Kyle Anderson Oct 24 '18 at 18:26
  • How hard is it to install `/usr/local/sbin/haproxy` without then running the service? Say as part of a CI/presubmit process or on a dev machine? -- It *looks* like `sudo service haproxy stop ; sudo systemctl disable haproxy.service` should do the job but I don't know for sure. – BCS Apr 06 '20 at 18:22
  • @BCS - you should ask a separate question, but simply running `sudo systemctl disable --now haproxy` would likely be the answer. ;) – dannysauer May 17 '20 at 12:57
  • 1
    Note that if you have TLS certificates with strict access like you should, you may need to run `sudo haproxy -c -V -f /etc/haproxy/haproxy.cfg` to check the file or you'll get errors about not being able to read TLS private keys. – Mikko Rantalainen Jan 28 '22 at 09:44
47

We are using this command sudo haproxy -f /etc/haproxy/haproxy.cfg -c

user854301
  • 5,383
  • 3
  • 28
  • 37