Hi - does anyone have a log file rotation script for nginx they can
share? Basically just looking for something simple I can set up in cron
once a day that will tell nginx to roll the log file and set its file
name to the current date, and then gzip it. Thanks!
I wrote this a while ago which does the trick
#!/bin/bash
YEAR=date "+%Y"
MONTH=date "+%m"
DAY=date "+%d"
HOSTNAME=hostname -s
LOG_FILES=“access.log error.log”
DATE=$YEAR/$MONTH/$DAY
LOG_ROOT=/var/log
NGINX_LOG_ROOT=$LOG_ROOT/nginx
make path
mkdir -p $NGINX_LOG_ROOT/$DATE
touch and symlink in new log files
for FILE in $LOG_FILES; do
LOG_FILE=$NGINX_LOG_ROOT/$DATE/$HOSTNAME.$FILE
touch $LOG_FILE
ln -fs $LOG_FILE $NGINX_LOG_ROOT/$FILE
done
tell nginx to re-open its log files
kill -USR1 cat /var/run/nginx.pid
i did almost exactly the same for a virtual-hosted server with loads
of logs in e.g. /var/www/$HOSTNAME/logs
#!/bin/sh
nginx_log_rotate.sh
Rotates nginx logs into hour-stamped archive directories
and gzips with to-the-second timestamps
NGINX_DIR=/path/to/nginx-install
PID_FILE=$NGINX_DIR/logs/nginx.pid
for s in /path/to/web/root/*
do
echo $s
if [ -L $s ] ; then # ignore symlinks, if any
continue
fi
log_dir=$s/logs
date_dir=`date +%Y/%m/%d/%H` # change this to the precision
you want, and match with crontab frequency
if [ -r ${log_dir}/access_log ] && [ -r ${log_dir}/
error_log ] ; then
# make new timed dir
/bin/mkdir -p ${log_dir}/${date_dir} > /dev/null 2>&1
# move in current logs
/bin/mv ${log_dir}/access_log ${log_dir}/${date_dir}/
access_log
/bin/mv ${log_dir}/error_log ${log_dir}/${date_dir}/
error_log
# tell nginx to reopen logs
kill -USR1 `cat $PID_FILE`
# compress old
compress_stamp=`date +%Y%m%d-%H%M%S`
/bin/gzip -S _$compress_stamp.gz ${log_dir}/$
{date_dir}/access_log &
/bin/gzip -S _$compress_stamp.gz ${log_dir}/$
{date_dir}/error_log &
fi
done
We use logrotate and things work just fine.
/var/log/engineyard/nginx/*.log {
daily
missingok
compress
rotate 30
dateext
notifempty
sharedscripts
extension gz
postrotate
[ ! -f /var/run/nginx.pid ] || kill -USR1 cat /var/run/nginx.pid
endscript
}
AmI missing something or using logrotate as Corey is just fine?
(I use logrotate for everything)
–
Aíbal
There is no log rotate on OS X by default, rather than figure out how
to install it and deal with its vagaries, this 5 minute script does
the job.
Rt, FWIW, with my script any pending log entires are written to the
previous days log file at rollover, none are lost.
Hi,
I have Nginx setup as a proxy for YAWS. It’s working fine and it’s
adding
the expires headers but for some reason it’s not gzipping the proxied
CSS
files.
Am I missing something? I’ve googled around for the last half hour but
it’s
unclear if this is supported or if I have to have YAWS do the
compression.
Thanks for your help!
John
You should be able to check the application type that’s returned by
your css files and use the gzip_types option in nginx.conf. Nginx
should be able to do the compression if you’re proxying down to
another web server just fine.