nginx 日志之 access_log分割
如果任由访问日志写下去,日志文件会变得越来越大,甚至是写满磁盘。
所以,我们需要想办法把日志做切割,比如每天生成一个新的日志,旧的日志按规定时间删除即可。
实现日志切割可以通过写shell脚本或者系统的日志切割机制实现。
shell脚本切割Nginx日志
切割脚本内容:
#!/bin/bash
logdir=/var/log/nginx #定义日志路径
prefix=`date -d "-1 day" +%y%m%d` #定义切割后的日志前缀
cd $logdir
for f in `ls access.log`
do
mv $f $f-$prefix #把日志改名
done
/bin/kill -USR1 $(cat /usr/local/nginx/logs/nginx.pid >/dev/null) >/dev/null #生成新的日志
bzip2 *$prefix #压缩日志
find . -type f -mtime + |xargs /bin/rm -f #删除超过180天的老日志
示例:
#!/bin/bash
logdir=/usr/local/nginx/logs/
prefix=`date -d "-1 day" +%y%m%d`
cd $logdir
for f in `ls access.log`
do
mv $f $prefix-$f
done
/bin/kill -USR1 $(cat /usr/local/nginx/logs/nginx.pid >/dev/null) >/dev/null
find . -type f -mtime + |xargs /bin/rm -f
系统日志切割机制
在/etc/logrotate.d/下创建nginx文件,内容为:
/data/logs/access.log {
daily
rotate 30
missingok
notifempty
compress
sharedscripts
postrotate
/bin/kill -USR1 $(cat /usr/local/nginx/logs/nginx.pid 2>/dev/null) 2>/dev/null || :
endscript
} 说明:
nginx日志在/data/logs/目录下面,日志名字以log结尾
daily表示每天切割
rotate 30表示日志保留30天
missingok表示忽略错误
notifempty表示如果日志为空,不切割
compress表示压缩
sharedscripts和endscript中间可以引用系统的命令
postrotate表示当切割之后要执行的命令
附:
1.如何测试logrotate程序执行的情况
答:/usr/sbin/logrotate -d /etc/logrotate.d/nginx
2.怎么查看log文件的具体执行情况
答:cat /var/lib/logrotate/status
3.使用-v或-d参数时,显示log does not need rotating
答:logrotate在对status未记录的文件进行转储时,会在status添加一条该文件的记录,并将操作时间设为当天。之后程序再次对此文件进行转储时发现这个文件今天已经操作过,就不再进行相关操作。
解决方法:
1. vi /var/lib/logrotate/status 更改相对应的文件操作日期
2. 使用-s指定状态文件
3.分割日志时报错:error: skipping "/var/log/nginx/test.access.log" because parent directory has insecure permissions (It's world writable or writable by group which is not "root") Set "su" directive in config file to tell logrotate which user/group should be used for rotation.
答:添加“su root list”到/etc/logrotate.d/nginx文件中即可
如下:
/var/log/nginx/*.log {
su root list
daily
missingok
rotate 52
compress
delaycompress
notifempty
#ifempty
create 0640 www-data adm
sharedscripts
postrotate
[ ! -f /var/run/nginx.pid ] || kill -USR1 `cat /var/run/nginx.pid`
endscript
}
nginx 日志之 access_log分割的更多相关文章
- Linux nginx日志按天分割实例
Linux nginx日志按天分割实例 nginx的日志有个小缺点,日志文件一直就是一个,不会自动地进行切割,如果访问量很大的话,将导致日志文件非常大,不便于管理这就需要我们自己来实现了,按日期每 ...
- Nginx 日志文件 access_log详解及日志分割
Module ngx_http_log_module nginx 日志相关指令主要有两条, 一条是log_format,用来设置日志格式,另外一条是access_log,用来指定日志文件的存放路径.格 ...
- Nginx 日志按天分割
#nginx日志切割脚本 #!/bin/bash #设置日志文件存放目录 logs_path="/home/www.xxx.com/wwwlogs/" #设置pid文件 pid_p ...
- Nginx日志按天分割
核心思想:使用crontab在每日23:59执行日志分割. 1.配置nginx日志信息,vim /etc/logrotate.d/nginx /var/log/nginx/*.log { nocomp ...
- Nginx 日志文件 access_log 详解
Module ngx_http_log_module nginx 日志相关指令主要有两条, 一条是log_format,用来设置日志格式,另外一条是access_log,用来指定日志文件的存放路径.格 ...
- nginx 日志之 access_log格式
Nginx访问日志可以设置自定义的格式,来满足特定的需求. 示例: 示例1 log_format combined_realip '$remote_addr $http_x_forwarded_for ...
- nginx 日志之 access_log
web服务器的访问日志是非常重要的,我们可以通过访问日志来分析用户的访问情况, 也可以通过访问日志发现一些异常访问,比如cc攻击. 格式: access_log /path/to/logfile fo ...
- nginx 日志之 access_log过滤
一个网站,会包含很多元素,尤其是有大量的图片.js.css等静态元素.这样的请求其实可以不用记录日志. 配置示例: location ~* ^.+\.(gif|jpg|png|css|js)$ { a ...
- nginx日志切割总结
Nginx日志切割 方法1(脚本+定时执行): #step1:加脚本 cut_nginx_log.sh,主进程把USR1信号发给worker,worker接到这个信号后,会重新打开日志文件 #!/ ...
随机推荐
- windows环境中hbase源码编译遇到的问题
转载请注明出处 问题一 [ERROR] Failed to execute goal org.codehaus.mojo:findbugs-maven-plugin:3.0.0:findbugs (d ...
- VS删除空白行
使用正则表达式, 搜索 (?<=\r\n)\r\n 替换空白
- Spring-Boot-操作-Redis,三种方案全解析!
在 Redis 出现之前,我们的缓存框架各种各样,有了 Redis ,缓存方案基本上都统一了,关于 Redis,松哥之前有一个系列教程,尚不了解 Redis 的小伙伴可以参考这个教程: Redis 教 ...
- 2019-11-29-WPF-客户端开发需要知道的触摸失效问题
原文:2019-11-29-WPF-客户端开发需要知道的触摸失效问题 title author date CreateTime categories WPF 客户端开发需要知道的触摸失效问题 lind ...
- Regex 常见语法
常用元字符 . 匹配除换行符以外的任意字符. \w 匹配字母或数字或下划线或汉字.\W 匹配任意不是字母,数字,下划线,汉字的字符. \s 匹配任意的空白符.\S 匹配任意不是空白符的字符.等价于 [ ...
- 使用order by和group by的分析
mysql 写sql的顺序: select -> from-> where->group by->having->order by. 但mysql的解析 ...
- linux中上传文件出现Refused to display 'http://***' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'.
刚开始出现这个问题,去百度了一下,网上很多答案都是一致的,但是在我这里没有解决问题.这个问题服务器没有打印日志.说明没有访问到服务器上,但是前端又没有问题,很奇怪,后面终于有一个同事和我说会不会是腾讯 ...
- 从零开始一步一步搭建Ubuntu Server服务器、修改数据源、安装Docker、配置镜像加速器、Compose部署Gitlab服务
场景 最终目的是使用Docker Compose部署一个Gitlab服务. 效果 注: 博客: https://blog.csdn.net/badao_liumang_qizhi关注公众号 霸道的程序 ...
- SQOOP的使用方法
Sqoop是个命令行工具,用来在Hadoop和rdbms之间传输数据. 以Hadoop的角度看待数据流向,从rdbms往Hadoop是导入用sqoop import命令,反之从hadoop往rdbms ...
- kibana自动创建索引
一般索引按月.季或年为单位创建索引.我这里写成logstash-www-2019-03,www是URL的二级域名.格式类型完全根据自己方便就行. 当ELK集群中的索引过多时,我这里有100多个不同的日 ...