Nginx - ?? logrotate ??????

时间:2020-08-28

????????????????? Yum ?? nginx ??????????????????????? ngx ??? ???????????????

 

 

????????????? shell ?? + crond ??????????? yum install nginx -y ??????????????????????????

 

Logrotate ??????#

??????????? Logrotate ?????????????? logrotate ??

?????????/etc/cron.daily/logrotate  ?????????? crond ??????

 

????????? cron ???logrotate ?????????????/etc/logrotate.conf

logrotate.conf ????????

 

?????????????????????????????????? include /etc/logrotate.d  ?????????????/etc/logrotate.d/ ? logrotate????????????????????

?? /etc/logrotate.d/

 

 

/etc/logrotate ??????? ????? nginx ?php-fpm????? zabbix-agent ? ??? nginx

 

 

??? ??????????? shell ???????????????????????????

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/var/log/nginx/*log {   # ????????
    create 0664 nginx root  # ???????????????
    daily   # ???????? daily: ???monthly: ???weekly: ???yearly: ??
    rotate 10   # ?????10?????????11??????????????
    missingok   # ?????????????????????????? ?????
    notifempty  # ?????????????????
    compress    # ?????????????gzip??
    sharedscripts   # ???? postrotate ??????????????
    postrotate  # ????
        /bin/kill -USR1 `cat /run/nginx.pid 2>/dev/null` 2>/dev/null || true  # ????
    endscript   # ????
}
 
sharedscripts / postrotate / endscript
???????????????????????????????????
 
???????????????????
 
dateext: ??????????

 

????? logrotate ?????????????????????

 

??????? nginx ???? logrotate ???

 

 

? ngx ?? ???????

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@192.168.118.11 ~]#cat /etc/logrotate.d/nginx
/usr/local/nginx/logs/*log {
    create 0664 nginx root
    daily
    dateext
    rotate 10
    missingok
    notifempty
    compress
    sharedscripts
    postrotate
        /bin/kill -USR1 `cat /usr/local/nginx/logs/nginx.pid 2>/dev/null` 2>/dev/null || true
    endscript
}

 

?????????nginx ???????????????????? shell????????????????????

?????????????????????? debug ?????

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@192.168.118.11 ~]#logrotate -d -f /etc/logrotate.d/nginx
reading config file /etc/logrotate.d/nginx
Allocating hash table for state file, size 15360 B
 
Handling 1 logs
 
rotating pattern: /usr/local/nginx/logs/*log  forced from command line (10 rotations)
empty log files are not rotated, old logs are removed
considering log /usr/local/nginx/logs/access.log
  log needs rotating
considering log /usr/local/nginx/logs/error.log
  log does not need rotating (log is empty)rotating log /usr/local/nginx/logs/access.log, log->rotateCount is 10
dateext suffix '-20200426'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
glob finding old rotated logs failed
renaming /usr/local/nginx/logs/access.log to /usr/local/nginx/logs/access.log-20200426
creating new /usr/local/nginx/logs/access.log mode = 0664 uid = 80 gid = 0
running postrotate script
running script with arg /usr/local/nginx/logs/*log : "
        /bin/kill -USR1 `cat /usr/local/nginx/logs/nginx.pid 2>/dev/null` 2>/dev/null || true
"
compressing log with: /bin/gzip

????? error ?????????????????????????????????

1
[root@192.168.118.11 ~]#logrotate -f /etc/logrotate.d/nginx

????????????????????????

 

 

???? logrotate???????? access.log ???? access.log-20200426 ????? gzip ??

????? error.log ?????????????? /etc/logrotate.d/nginx ??????? notifempty # ??????????????????

 

??#

??????nginx ???????????? shell??????????????????????????????? logrotate ????


【推荐】 Nginx - 使用 logrotate 进行日志切割 - hukey - 博客园
【推荐】 nginx+ffmpeg搭建流媒体服务器(直播流)
【推荐】 网站访问量pv统计-排除爬虫
【推荐】 linux常用操作
【推荐】 30分钟学懂K8s--k8s教程

码字不易,如果您觉得文章写得不错,您又有闲心的话,请点击广告支持