我正在嘗試在運行 HAProxy 的 Fedora 22 伺服器上設定 SSL。這是配置(/etc/haproxy/haproxy.cfg
):
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
tune.ssl.default-dh-param 2048
defaults
log global
mode http
option httplog
option dontlognull
option http-server-close
option forwardfor
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
frontend main
bind *:80
bind *:443 ssl crt /etc/haproxy/certificate.pem
redirect scheme https if !{ ssl_fc }
default_backend app
backend app
balance roundrobin
server app1 127.0.0.1:8000 check
現在,當我運行時systemctl restart haproxy && journalctl -u haproxy.service -f
,我收到此錯誤:
Sep 13 15:39:31 fedora-server systemd[1]: Started HAProxy Load Balancer.
Sep 13 15:39:31 fedora-server systemd[1]: Starting HAProxy Load Balancer...
Sep 13 15:39:31 fedora-server haproxy-systemd-wrapper[15620]: haproxy-systemd-wrapper: executing /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds
Sep 13 15:39:31 fedora-server haproxy-systemd-wrapper[15620]: [ALERT] 255/153931 (15621) : parsing [/etc/haproxy/haproxy.cfg:30] : 'bind *:443' : unable to load SSL private key from PEM file '/etc/haproxy/certificate.pem'.
Sep 13 15:39:31 fedora-server haproxy-systemd-wrapper[15620]: [ALERT] 255/153931 (15621) : Error(s) found in configuration file : /etc/haproxy/haproxy.cfg
Sep 13 15:39:31 fedora-server haproxy-systemd-wrapper[15620]: [ALERT] 255/153931 (15621) : Proxy 'main': no SSL certificate specified for bind '*:443' at [/etc/haproxy/haproxy.cfg:30] (use 'crt').
Sep 13 15:39:31 fedora-server haproxy-systemd-wrapper[15620]: [ALERT] 255/153931 (15621) : Fatal errors found in configuration.
Sep 13 15:39:31 fedora-server haproxy-systemd-wrapper[15620]: haproxy-systemd-wrapper: exit, haproxy RC=256
這是服務配置:
# cat /usr/lib/systemd/system/haproxy.service
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target
[Service]
EnvironmentFile=/etc/sysconfig/haproxy
ExecStart=/usr/sbin/haproxy-systemd-wrapper -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid $OPTIONS
ExecReload=/bin/kill -USR2 $MAINPID
[Install]
WantedBy=multi-user.target
但是,我可以複製該服務嘗試運行的命令,並且它工作正常。
首先,當我手動運行它時(從服務配置中獲取),這是有效的:
# whoami
root
# /usr/sbin/haproxy-systemd-wrapper -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid
這也有效(取自服務日誌,顯然haproxy-systemd-wrapper
運行此:
# whoami
root
# /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds
澄清一下:當我以 root 身分手動執行這些命令時,它們會起作用,並且我可以透過 SSL 訪問我的網站。
因此,我假設當作為服務運行時,HAProxy 無法讀取憑證。
到目前為止,這是我嘗試過的:
chown haproxy:haproxy /etc/haproxy/certificate.pem
- 將使用者和群組切換為root
haproxy.cfg
- 新增
User=root
和Group=root
到服務配置中[Service]
sudo -u haproxy /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds
給予不同的錯誤(關於連接埠),因此服務很可能不使用使用者 haproxy,否則會進一步
我可以以使用者 haproxy 的身分存取該檔案sudo -u haproxy cat /etc/haproxy/certificate.pem
。
編輯:
這是根據您的回答更新的服務配置:
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target
SELinuxContext=unconfined_u:object_r:var_t:s0
現在發生了什麼事:
Sep 13 17:51:21 ServiceName systemd[1]: Starting HAProxy Load Balancer...
Sep 13 17:51:21 ServiceName systemd[1]: haproxy.service: main process exited, code=exited, status=203/EXEC
Sep 13 17:51:21 ServiceName systemd[1]: Unit haproxy.service entered failed state.
Sep 13 17:51:21 ServiceName systemd[1]: haproxy.service failed.
我如何獲得以下值SELinuxContext
:
# ls -lZ /etc/haproxy/certificate.pem
-rw-r--r--. 1 root root unconfined_u:object_r:var_t:s0 9245 Sep 13 17:43 /etc/haproxy/certificate.pem
答案1
您使用的是 Fedora,它使用SELinux。 Systemd 服務在乾淨的環境中運作 – 它們是不是由 systemctl 直接生成——所以除其他外,它們在不同的 SELinux 上下文中啟動(我認為預設是system_u:system_r:init_t:s0
?)。
確保為證書檔案(使用ls -lZ
和chcon
)和 haproxy 進程(可能SELinuxContext=
在 systemd 單元中使用)設定正確的 SELinux 上下文。
嘗試運行id
以查看您(或服務)的當前上下文。