LNMP架构(三)之Nginx访问日志,切割,静态文件不记录日志

Nginx访问日志nginx

日志格式 vim /usr/local/nginx/conf/nginx.conf //搜索log_formatshell

输入图片说明

除了在主配置文件nginx.conf里定义日志格式外,还须要在虚拟主机配置文件中增长vim

access_log /tmp/1.log combined_realip;      //这里引用的主配置文件定义的日志格式名称
这里的combined_realip就是在nginx.conf中定义的日志格式名字
-t && -s reload
curl -x127.0.0.1:80 test.com -I
cat /tmp/1.log

输入图片说明

Nginx日志切割bash

自定义shell 脚本curl

vim /usr/local/sbin/nginx_log_rotate.sh//写入以下内容
#! /bin/bash
## 假设nginx的日志存放路径为/data/logs/
d=`date -d "-1 day" +%Y%m%d`
logdir="/data/logs"
nginx_pid="/usr/local/nginx/logs/nginx.pid"
cd $logdir
for log in `ls *.log`
do
    mv $log $log-$d
done
/bin/kill -HUP `cat $nginx_pid`

输入图片说明

任务计划url

0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh
还能够加上日志清理任务,每月1号凌晨清理
0 0 1 1 * find  /tmp/ -type f -mtime +30 -exec rm -f {} \;
* */25 * * * echo "test cront" /var/log/test_cront.txt

Nginx静态文件不记录日志和过时时间日志

输入图片说明

相关文章
相关标签/搜索