In Linux, nginx logs are scheduled to cut instances on a daily basis.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.