Skip to content

Commit

Permalink
entrypoint: Monitor config dir for changes
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
oliv3r committed Jun 4, 2023
1 parent 10fa7fc commit ff48fcb
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Dockerfile-alpine.template
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,5 @@ RUN set -x \
&& if [ -n "/etc/apk/keys/nginx_signing.rsa.pub" ]; then rm -f /etc/apk/keys/nginx_signing.rsa.pub; fi \
# Bring in curl and ca-certificates to make registering on DNS SD easier
&& apk add --no-cache curl ca-certificates
# Add support for manually monitoring files to trigger server reloads
&& apk add --no-cache inotify-tools
44 changes: 44 additions & 0 deletions entrypoint/99-monitor-config-changes.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/sh
# vim:sw=2:ts=2:sts=2:et

set -eu
if [ -n "${DEBUG_TRACE_SH:-}" ] && \
[ "${DEBUG_TRACE_SH:-}" != "${DEBUG_TRACE_SH#*"$(basename "${0}")"*}" ] || \
[ "${DEBUG_TRACE_SH:-}" = 'all' ]; then
set -x
fi

LC_ALL=C

if [ -e "${NGINX_ENTRYPOINT_MONITOR_PID:=/run/nginx_monitor.pid}" ] ||
[ -z "${NGINX_ENTRYPOINT_MONITOR_CONFIG+monitor}" ] || \
! command -v inotifywait; then
exit 0
fi

echo "Monitoring for changes in '${NGINX_ENTRYPOINT_MONITOR_CONFIG:=/etc/nginx}'"
while true; do
inotifywait \
--recursive \
--event 'create' \
--event 'delete' \
--event 'modify' \
--event 'move' \
"${NGINX_ENTRYPOINT_MONITOR_CONFIG}"

sleep "${NGINX_ENTRYPOINT_MONITOR_DELAY:-10s}"

if [ ! -e "${NGINX_ENTRYPOINT_MONITOR_PID}" ]; then
logger -s -t 'nginx' -p 'local0.3' 'Monitor failure or exit requested'
break
fi

if nginx -t; then
nginx -s
else
logger -s -t 'nginx' -p 'local0.3' 'Refusing to reload config, config error'
fi
done &
echo "${!}" > "${NGINX_ENTRYPOINT_MONITOR_PID}"

exit 0

0 comments on commit ff48fcb

Please sign in to comment.