我是Linux新手,正在尝试使用环境variables来configurationHAProxy。 我正在运行Debian 8(Jessie)和HAProxy 1.5.8,我使用apt-get安装,按照https://haproxy.debian.net/#?distribution=Debian&release=jessie&version=1.5
没有环境variables,configuration工作非常好。
我遵循这个答案 (从/etc/init.d/haproxy设置环境variables)的指示来获取环境variables的工作,但仍然得到了503 – 服务不可用的响应。
然后我查看了/etc/init.d/haproxy并认为这两行可能会改变可用variables的范围
[ -f /etc/default/rcS ] && . /etc/default/rcS . /lib/lsb/init-functions
所以我评论他们。
然后我可以启动HAProxy使用sudo /etc/init.d/haproxy start ,我的configuration工作正如我所料,但是当我尝试运行sudo service haproxy start我仍然得到503 – 服务不可用。
在我看来, 这个问题有我正在寻找的答案,但我还没有能够把答案从那里翻译到我的域名。
这是我使用环境variables的haproxy.cfg文件的一部分:
backend backend1 server EnvVar ${SERVER_IP}:86 maxconn 512
我也在这里查看了一些其他的问题,但没有find解决我的问题的办法。
当haproxy作为服务运行时,如何设置环境variables以便haproxy可以读取它?
我终于弄明白了。
在/lib/systemd/system/haproxy.service有这样一行
EnvironmentFile=-/etc/default/haproxy
我查看了/etc/default/haproxy ,这就是它的样子:
# Defaults file for HAProxy # # This is sourced by both, the initscript and the systemd unit file, so do not # treat it as a shell script fragment. # Change the config file location if needed #CONFIG="/etc/haproxy/haproxy.cfg" # Add extra flags here, see haproxy(1) for a few options #EXTRAOPTS="-de -m 16"
我在那里添加了我的环境variables,并且还原了其余的更改,并且在作为服务运行时以及在使用/etc/init.d/haproxy直接运行它时都起作用。
这是我添加的行:
SERVER_IP=165.88.76.44
这个答案是正确的,并在haproxy 1.7 – 1.8上正常工作
这里有一个简单的脚本来改善多个haproxy文件的使用:
cd /etc/sysconfig for i in haproxy_*; do echo $i; echo "MY_SERVER=$(hostname -i)" >> $i done
现在在Haproxy设置:
... backend lochttps server server_name ${MY_SERVER}:60441 check ssl verify none id 1 ...