Skip to content

Commit 4427d51

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` 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, and can't be easily killed. This isn't a problem however, as this is specifically a docker entry point and it is fair to assume this will only ever be run under docker. 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 4427d51

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-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
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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=/etc/nginx}" ] || \
15+
! command -v inotifywait; then
16+
exit 0
17+
fi
18+
19+
echo "Monitoring for changes in '${NGINX_ENTRYPOINT_MONITOR_CONFIG}'"
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 nginx -t; then
32+
nginx -s
33+
else
34+
logger -s -t 'nginx' -p 'local0.3''Refusing to reload config, config error'
35+
fi
36+
done &
37+
echo "${!}" > "${NGINX_ENTRYPOINT_MONITOR_PID}"
38+
39+
exit 0

0 commit comments

Comments
 (0)