Skip to content

Commit ff48fcb

Browse files
committed
entrypoint: Monitor config dir for changes
We see a lot of crudges and hacks to notify nginx or the nginx container informing it it needs to restart. While there certainly cases that require manual control, for the most, this could be easily automated. With inotify, we can recursively monitor /etc/nginx (or any directory per config) for changes (currently, not monitoring for for access time changes, e.g. reads or `touch` (not creating new files) events). On an event, we sleep first for (configurable) seconds, the default is 10, so that multiple updates don't cause multiple restarts. E.g. copying 10 certificates into /etc/nginx/certs, won't trigger 10 reloads. The monitor will run indefinably, but to ensure there is 'some' way to exit it, is to remove the pid file (configurable location) and triggering a `/etc/nginx` change (`touch '/etc/nginx/exit'` for example to create a file. It's not perfect, but probably will never be used anyway. The current configuration won't change existing behavior, it needs to be explicitly enabled. Signed-off-by: Olliver Schinagl <[email protected]>
1 parent 10fa7fc commit ff48fcb

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

Dockerfile-alpine.template

+2
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,5 @@ RUN set -x \
7676
&& if [ -n "/etc/apk/keys/nginx_signing.rsa.pub" ]; then rm -f /etc/apk/keys/nginx_signing.rsa.pub; fi \
7777
# Bring in curl and ca-certificates to make registering on DNS SD easier
7878
&& apk add --no-cache curl ca-certificates
79+
# Add support for manually monitoring files to trigger server reloads
80+
&& apk add --no-cache inotify-tools
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/bin/sh
2+
# vim:sw=2:ts=2:sts=2:et
3+
4+
set -eu
5+
if [ -n "${DEBUG_TRACE_SH:-}" ] && \
6+
[ "${DEBUG_TRACE_SH:-}" != "${DEBUG_TRACE_SH#*"$(basename "${0}")"*}" ] || \
7+
[ "${DEBUG_TRACE_SH:-}" = 'all' ]; then
8+
set -x
9+
fi
10+
11+
LC_ALL=C
12+
13+
if [ -e "${NGINX_ENTRYPOINT_MONITOR_PID:=/run/nginx_monitor.pid}" ] ||
14+
[ -z "${NGINX_ENTRYPOINT_MONITOR_CONFIG+monitor}" ] || \
15+
! command -v inotifywait; then
16+
exit 0
17+
fi
18+
19+
echo "Monitoring for changes in '${NGINX_ENTRYPOINT_MONITOR_CONFIG:=/etc/nginx}'"
20+
while true; do
21+
inotifywait \
22+
--recursive \
23+
--event 'create' \
24+
--event 'delete' \
25+
--event 'modify' \
26+
--event 'move' \
27+
"${NGINX_ENTRYPOINT_MONITOR_CONFIG}"
28+
29+
sleep "${NGINX_ENTRYPOINT_MONITOR_DELAY:-10s}"
30+
31+
if [ ! -e "${NGINX_ENTRYPOINT_MONITOR_PID}" ]; then
32+
logger -s -t 'nginx' -p 'local0.3' 'Monitor failure or exit requested'
33+
break
34+
fi
35+
36+
if nginx -t; then
37+
nginx -s
38+
else
39+
logger -s -t 'nginx' -p 'local0.3' 'Refusing to reload config, config error'
40+
fi
41+
done &
42+
echo "${!}" > "${NGINX_ENTRYPOINT_MONITOR_PID}"
43+
44+
exit 0

0 commit comments

Comments
 (0)