Sometimes our servers stop doing what we expect them to do.
Often this is due to some automatic update that changes a config.
Sometimes it's just Plesk reverting some custom changes.
I already have some scripts in place that checks the folder /etc/postfix or /etc/dovecot for changes and that helped me a lot when troubleshooting it.
Because the Nginx config is built from many .conf files it's better to take a different approach.
I'm using the command "nginx -T" that outputs the resulting config.
When that resulting config is different than the latest stored config it will create a new one.
Using the "diff" command on these files will quickly lead you to the answer why something is now different:
ln -s /usr/local/sbin/nginx_backup /etc/cron.hourly/
cat /usr/local/sbin/nginx_backup
Often this is due to some automatic update that changes a config.
Sometimes it's just Plesk reverting some custom changes.
I already have some scripts in place that checks the folder /etc/postfix or /etc/dovecot for changes and that helped me a lot when troubleshooting it.
Because the Nginx config is built from many .conf files it's better to take a different approach.
I'm using the command "nginx -T" that outputs the resulting config.
When that resulting config is different than the latest stored config it will create a new one.
Using the "diff" command on these files will quickly lead you to the answer why something is now different:
ln -s /usr/local/sbin/nginx_backup /etc/cron.hourly/
cat /usr/local/sbin/nginx_backup
Code:
#!/bin/bash
FOLDER=/etc/nginx
DATESTAMP=`date +%Y-%m-%d.%H-%M`
NEWBACKUP=${FOLDER}/nginx.conf.${DATESTAMP}
TMPDIR=`mktemp -t -d ${0//*\/}.XXXXXXXXXX`
LATESTBACKUP="`ls -1t --color=none ${FOLDER}/nginx.conf.* | grep '[0-9][0-9]\-[0-9][0-9]$' | head -n1`"
nginx -T 2>/dev/null >${TMPDIR}/current
if [ -z "${LATESTBACKUP}" ] ; then
mv ${TMPDIR}/current ${NEWBACKUP}
elif ! diff ${TMPDIR}/current ${LATESTBACKUP} 2>&1 >/dev/null ; then
echo "Nginx configuration has changed, new one stored in ${NEWBACKUP}"
mv ${TMPDIR}/current ${NEWBACKUP}
fi
rm -r ${TMPDIR}