I've found an example of doing log rotation in Nginx here
But a simple test with:
set $date "2018-08-24";
access_log /home/tim/log/access-http-$date.log default;
produces a log file named access-http-.log
. I'm using nginx 1.13.6 (with openresty).
Update
After much hacking & tweaking, I've come up with the following logrotate script to rotate the different log files that nginx produces. I've put it into /etc/logrotate. The remaining issue is that the logs don't rotate daily (I'm unsure why at present), but a logrotate -f <filename>
produces exactly the result I want.
An interesting note is that nginx -s reload
can be used instead of USR1. That's unfortunately not referenced in the nginx logging page but I found it (in the man page IIRC). I build openresty/nginx manually to incorporate extra modules I need so I don't get various extras that come in the packaged version like the pid keeping.
/path/to/log/access-http.log /path/to/log/access-https.log /path/to/log/api.log /path/to/log/error.log {
daily
missingok
notifempty
create 664 nobody tim
dateext
dateformat -%Y-%m-%d
dateyesterday
rotate 10
su tim tim
postrotate
/usr/local/bin/openresty -s reload
endscript
}
I figure this will be useful for anyone with a large nginx config serving both web pages and an API. I keep the http separate as I don't serve non-https pages and it keeps the script kiddie crap out of my page logs.
Just set a cron task for a minute to midnight to move the logfile and rename it with the date and then send a USR1 signal to Nginx. This will trigger it to reopen log files and create a new one for the following day.