Validate haproxy.cfg
Asked Answered
W

2

82

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.

Williawilliam answered 21/9, 2016 at 6:42 Comment(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.Iridis
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.Williawilliam
W
153

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.

Williawilliam answered 7/11, 2016 at 11:56 Comment(5)
sudo service haproxy configtest is not a recognize option for me with the last haproxyIncrust
/usr/sbin/haproxy -c -V -f /etc/haproxy/haproxy.cfg works for me. Returns "Configuration file is valid"Parable
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.Gillian
@Gillian - you should ask a separate question, but simply running sudo systemctl disable --now haproxy would likely be the answer. ;)Athalee
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.Lagging
V
47

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

Vanillic answered 7/9, 2017 at 11:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.