Nginx
是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP
服务器。在这里就不用过多解释了,本文主要讲述一下如何实现自动切割Nginx产生的日志文件。
1、使用vi或vim命令新建需要切割日志的shell文件脚本,具体命令如下:
[root@iZ256w2hluuZ ~]# vi /data/nginx/cut_nginx_log.sh
在shell
文件中增加如下内容:
#!/bin/bash function cutLog() { 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 } cutLog "/home/wwwlogs/www.yoodb.com/" cutLog "/home/wwwlogs/www.yoodb-1.com/" cutLog "/home/wwwlogs/www.yoodb-2.com/" cutLog "/home/wwwlogs/www.yoodb-3.com/" # 重启 nginx kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
2、加入定时任务,每天凌晨执行自动切割任务脚本,具体命令如下:
[root@iZ256w2hluuZ ~]# crontab -e 0 0 * * * /data/nginx/cut_nginx_log.sh
关于使用 crond 命令在 Linux 系统下执行定时任务参考资料:https://blog.yoodb.com/yoodb/article/detail/1131
3、Nginx中日志文件格式在nginx.conf中定义,其默认格式如下:
log_format access '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" $http_x_forwarded_for'; access_log /home/wwwlogs/www.yoodb.com/access.log access; #新增
各个字段的含义如下:
1)$remote_addr
与$http_x_forwarded_for
用以记录客户端的ip地址;
2)$remote_user
:用来记录客户端用户名称;
3)$time_local
: 用来记录访问时间与时区;
4)$request
: 用来记录请求的url与http协议;
5)$status
: 用来记录请求状态;成功是200,
6)$body_bytes_s ent
:记录发送给客户端文件主体内容大小;
7)$http_referer
:用来记录从那个页面链接访问过来的;
8)$http_user_agent
:记录客户端浏览器的相关信息。
奉 (2018/01/16 16:59:52)回复
一辈子只做好一件事!我是有多么的大方吖!哈哈