sudo vi /etc/logrotate.d/nginx
/var/log/nginx/*.log
/var/log/nginx/project-1/*.log
/var/log/nginx/project-2/*.log
/var/log/nginx/project-3/*.log
/var/log/nginx/project-4/*.log {
su root root
daily
missingok
rotate 1
compress
delaycompress
notifempty
dateext
dateformat -%Y-%m-%d
copytruncate
sharedscripts
postrotate
if [ -f /app/nginx/run/nginx.pid ]; then
kill -USR1 `cat /app/nginx/run/nginx.pid`
fi
endscript
}
- daily: Rotate the logs daily.
- missingok: Do not issue an error if the log file is missing.
- rotate 14: Keep 14 days' worth of logs.
- compress: Compress the rotated log files.
- dateext: Append an extension with the date to the rotated log files.
- dateformat -%Y-%m-%d: Specify the date format for the log file names.
- delaycompress: Delay compression until the next rotation cycle.
- notifempty: Do not rotate the log if it is empty.
- create 0640 www-data adm: Create new log files with specified permissions and ownership.
- sharedscripts: Run the postrotate script only once, not for each log file.
- postrotate: Commands to run after log rotation. This sends a signal to Nginx to reopen the log files.
copytruncate
: This option works by copying the existing log file and then truncating (emptying) the original log file when rotating the logs. In other words, it replaces the log file without closing the currently open file handles. This allows the program to continue writing to the log file while keeping the file handles intact.
Test the logrotate configuration to ensure it is set up correctly:
sudo logrotate -d /etc/logrotate.d/nginx
The -d
option runs logrotate in debug mode, showing what would happen without actually rotating the logs.
After copying to the file
access.log-2024-06-26
, logs are again being accumulated in the originalaccess.log