Tutorial purpose:
Use the built-in Linux command logrotate to cut Nginx logs.
Nginx installation directory:/usr/local/nginx/
Nginx log Directory:/usr/local/nginx/logs/,/usr/local/nginx/logs/nginx_logs/
1. Add an nginx log cutting script
Cd/etc/logrotate. d # enter the Directory
Vi/etc/logrotate. d/nginx # edit the script
/Usr/local/nginx/logs/*. log/usr/local/nginx/logs/nginx_logs/*. log {
Missingok
Dateext
Notifempty
Daily
Rotate 7
Sharedscripts
Postrotate
If [-f/usr/local/nginx/logs/nginx. pid]; then
Kill-USR1 'cat/usr/local/nginx/logs/nginx. Pid'
Fi
Endscript
}
: Wq! # Save and exit
Chmod + x/etc/logrotate. d/nginx # Add execution permission
2. Execute the script
/Usr/sbin/logrotate-vf/etc/logrotate. d/nginx
3. Add a scheduled task
Crontab-e # add the following code
0 0 ***/usr/sbin/logrotate-vf/etc/logrotate. d/nginx # execute the script regularly every morning
Now, the daily scheduled cut tutorial for nginx logs in Linux is complete.
Remarks: logrotate parameters
Missingok: ignore errors, such as the error message "log file cannot be found.
Dateext: After the switch, the log file will be appended with a short horizontal line and a date in YYYYMMDD format. Without this configuration item, a decimal point and a numerical number will be appended.
Receivempty: If the log file is empty, no cutting is performed.
Daily: cut logs by day. Available value month: monthly week: weekly year: yearly
Rotate 7: keep the log records of the last 7 days
Sharedscripts: a script that runs only once for the entire log Group
Postrotate and endscript: The Specified Command will be executed.
Compress: after the round robin task is completed, the archive that has been round robin will be compressed using gzip.
Delaycompress: always used with the compress option. The delaycompress option instructs logrotate not to compress the latest archive. Compression will be performed in the next cycle. This is useful when you or any software still needs to read the latest archive.
Create 644 root: create a new log file with the specified permissions. logrotate also renames the original log file.