当前位置:   首页安装配置

切割日志:Linux下nginx生成日志自动切割的实现方法

发布日期:2022-01-20 11:14 | 文章来源:站长之家

Linux下nginx生成日志自动切割的实现方法

1、编辑切割日志的 shell 程序,目录自定

#vi /data/nginx/cut_nginx_log.sh 

输入代码:

#!/bin/bash 
             
# This script run at 00:00 
             
function cutAccess() 
{ 
  dir=$1 
  newdir="${dir}/$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")" 
  suffix=$(date -d "yesterday" +"%Y%m%d") 
  mkdir -p $newdir 
  mv ${dir}/access.log ${newdir}/access.$suffix.log 
} 
             
cutAccess "/home/wwwlogs/www.yourdomain.com/" 
cutAccess "/home/wwwlogs/www.yourdomain-1.com/" 
cutAccess "/home/wwwlogs/www.yourdomain-2.com/" 
           
# 重启 nginx 
kill -HUP `cat /usr/local/nginx/logs/nginx.pid` 

2、加入定时任务,每天0点自动切割

# crontab -e 
0 0 * * * /bin/bash /data/nginx/cut_nginx_log.sh 

3、nginx 日志格式

log_format access '$remote_addr - $remote_user [$time_local] "$request" ' 
      '$status $body_bytes_sent "$http_referer" ' 
      '"$http_user_age

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

联系我们
关于使用场景和技术架构的更多咨询,请联系我们的销售和技术支持团队。
Yingsoo Host

在线
客服

在线客服:7*24小时在线

客服
热线

400-630-3752
7*24小时客服服务热线

关注
微信

关注官方微信
顶部