Scheduled task Nginx log polling cutting
This environment is based on the Centos 7.8 system to build the Nginx learning environment. For
specific construction, please refer to Nginx-1.18.0 Environment Deployment
Nginx serves as a very powerful web server and reverse proxy server. Deploy to online businesses, especially websites that have seen a surge in online business volume. The Nginx server access volume is very large, and the huge access volume has also brought about the increase of log files, especially the access log of the Nginx service. All we need to plan the log polling and cutting of Nginx access logs.
The Nginx service yum is deployed online, and the log cutting has been automatically deployed, but the source code installation requires manual setting by the administrator
1. Log polling cutting in yum mode
View the default log cut file of the nginx service
[root@node01 ~]# vim /etc/logrotate.d/nginx
/var/log/nginx/*.log {
daily
missingok
rotate 52
compress
delaycompress
notifempty
create 640 nginx adm
sharedscripts
postrotate
if [ -f /var/run/nginx.pid ]; then
kill -USR1 `cat /var/run/nginx.pid`
fi
endscript
}
2. Log polling cutting in source code mode
1. Method one (configuration file)
logrotate itself runs as a scheduled task
[root@node02 ~]# vim /etc/logrotate.d/nginx
/usr/local/nginx/logs/*.log {
dateext
daily
missingok
rotate 30
compress
delaycompress
notifempty
create 640 nginx adm
sharedscripts
postrotate
if [ -f /usr/local/nginx/logs/nginx.pid ]; then
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
fi
endscript
}
test
[root@node02 ~]# logrotate -f /etc/logrotate.d/nginx
[root@node02 ~]# ll /usr/local/nginx/logs/
total 12
-rw-r----- 1 nginx adm 0 Feb 21 20:44 access_2021-02-21.log
-rw-r--r-- 1 nginx root 0 Feb 21 19:45 access.log
-rw-r----- 1 nginx adm 0 Feb 21 20:44 error.log
-rw-r--r-- 1 root root 620 Feb 21 19:45 error.log-20210221
-rw-r--r-- 1 root root 5 Feb 21 19:31 nginx.pid
2. Method two (service script + scheduled task)
Provide script
[root@node02 ~]# mkdir /scripts
[root@node02 ~]# cd /scripts
[root@node02 scripts]# vim cut_nginx_access_logs.sh
#!/bin/bash
# def var
date_format=$(date +%F)
log_dir=/usr/local/nginx/logs
log_name=access
# main program
[ -d ${log_dir} ] && cd ${log_dir} || exit 1
[ -f ${log_dir}/${log_name}.log ] || exit 1
mv ${log_dir}/${log_name}.log ${log_dir}/${log_name}_${date_format}.log
/usr/local/nginx/sbin/nginx -s reload
[root@node02 scripts]# chmod +x cut_nginx_access_logs.sh
[root@node02 scripts]# sh -n cut_nginx_access_logs.sh
[root@node02 scripts]# ./cut_nginx_access_logs.sh
[root@node02 scripts]# ll /usr/local/nginx/logs/
total 12
-rw-r--r-- 1 root root 614 Feb 21 16:41 access_2021-02-21.log
-rw-r--r-- 1 root root 0 Feb 21 19:45 access.log
-rw-r--r-- 1 root root 620 Feb 21 19:45 error.log
-rw-r--r-- 1 root root 5 Feb 21 19:31 nginx.pid
Configure scheduled tasks
[root@node02 scripts]# crontab -e
# cut nginx access logs by wan
00 03 * * * /bin/bash /scripts/cut_nginx_access_logs.sh &> /dev/null