运维之道 | Nginx日志切割实战

Nginx日志切割实战

一、Apache Benchmark(ab) 压力测试工具

[root@localhost nginx]# yum install -y httpd-tools
ab压力测试工具的使用
  • 模拟并发请求1次,总共请求10000次
  • 命令模板:
    ab -c 1 -n 10000 待测试网站(建议完整路径)/
1、使用du -sh *命令查看日志文本大小
[root@localhost nginx]# du -sh *
64K     access.log
84K     error.log
2、使用ab压力测试工具扩大日志文本大小
[root@localhost nginx]# ab -c 100 -n 10000 http://192.168.146.133/
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.146.133 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests


Server Software:        nginx/1.8.1
Server Hostname:        192.168.146.133
Server Port:            80

Document Path:          /
Document Length:        612 bytes

Concurrency Level:      100
Time taken for tests:   1.425 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      8440000 bytes
HTML transferred:       6120000 bytes
Requests per second:    7019.49 [#/sec] (mean)
Time per request:       14.246 [ms] (mean)
Time per request:       0.142 [ms] (mean, across all concurrent requests)
Transfer rate:          5785.59 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    3   1.3      3       8
Processing:     7   11  10.1     10     113
Waiting:        1    8  10.0      7     107
Total:          9   14  10.0     13     113

Percentage of the requests served within a certain time (ms)
  50%     13
  66%     13
  75%     13
  80%     14
  90%     14
  95%     15
  98%     24
  99%    112
 100%    113 (longest request)
3、再次通过du -sh *命令查看日志文本大小
[root@localhost nginx]# du -sh *
1.0M    access.log
84K     error.log

二、手动日志切割

1、查看当前目录中存在的日志
[root@localhost nginx]# ls
access.log  error.log
2、对前一天的日志进行备份
[root@localhost nginx]# date
20191214日 星期六 10:24:11 CST
[root@localhost nginx]# mv access.log `date +%F -d -1day`_access.log
[root@localhost nginx]# ls
2019-12-13_access.log  error.log

[root@localhost nginx]# mv error.log `date +%F -d -1day`_error.log       
[root@localhost nginx]# ls
2019-12-13_access.log  2019-12-13_error.log
3、重新打开新的nginx日志文件

命令:/usr/local/nginx/sbin/nginx -s reopen

[root@localhost nginx]# /usr/local/nginx/sbin/nginx -s reopen
[root@localhost nginx]# ls
2019-12-13_access.log  2019-12-13_error.log  access.log  error.log

以上为日志切割的主要思路步骤


三、企业自动日志切割实战

1、在日志目录中新建存放备份日志的目录
[root@localhost nginx]# mkdir oldlogs
[root@localhost nginx]# ls
access.log  error.log  oldlogs
2、编写日志备份脚本

思路步骤:

  • 创建当前时间目录,比如年/月目录
  • 然后将当前指定的日志,剪切到该创建好的目录下
  • 重启nginx服务,重新生成0kb大小的新日志文件
  • 通过计划任务,周期性执行以上切割日志,重新生成日志的操作就能达到既让日志文件保持一定大小而且又能按照指定的时间格式和目录进行存放。方面查询的便利和文件打开的快速。
#!/bin/bash

#原nginx日志存放路径
log_files_path="/var/log/nginx/"

#nginx切割后存放路径
log_files_dir=${log_files_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")

#待切割日志名称集合如access.log error.log
log_files_name=(access error)

#nginx启动脚本路径
nginx_sbin="/usr/local/nginx/sbin/nginx"

#日志保存时间
save_days=30

#创建存放目录
mkdir -p $log_files_dir

log_files_num=${#log_files_name[@]}

#循环切割日志
for((i=0;i<$log_files_num;i++));do
mv ${log_files_path}${log_files_name[i]}.log ${log_files_dir}/${log_files_name[i]}_$(date -d "yesterday" +"%Y%m%d").log
done

#删除超过30天的日志文件
find $log_files_path -mtime +$save_days -exec rm -rf {} \;

#重启nginx服务,重新生成新的文件
$nginx_sbin -s reload
3、执行日志切割脚本
[root@localhost nginx]# ls
2019  access.log  error.log  `nginx_cutting.sh`  nginx.pid

[root@localhost nginx]# sh nginx_cutting.sh 
[root@localhost nginx]# ls
2019  access.log  error.log  nginx_cutting.sh  nginx.pid	///产生2019年的目录

[root@localhost nginx]# cd 2019
[root@localhost 2019]# ls			///产生12月的目录
12

[root@localhost 2019]# cd 12
[root@localhost 12]# ls				///产生前一天的日志
access_20191213.log  error_20191213.log
4、授予该日志脚本可执行权限
[root@localhost nginx]# chmod +x nginx_cutting.sh
5、将日志切割脚本写入crontab计划任务中
[root@localhost nginx]# crontab -e
00 00 * * * /bin/bash /var/log/nginx/nginx_cutting.sh

Logrotate日志切割方法

发布了97 篇原创文章 · 获赞 10 · 访问量 3407

猜你喜欢

转载自blog.csdn.net/VillianTsang/article/details/103536389
今日推荐