您的位置:首页 > 运维架构 > Nginx

12.10 Nginx访问日志;12.11 Nginx日志切割;12.12 静态文件不记录日志和过期时间

2017-08-11 11:45 791 查看
12.10 Nginx访问日志
1. 编辑...nginx.conf主配置文件,自定义日志格式名称:
[root@hao-01 conf]# vim /usr/local/nginx/conf/nginx.conf
更改内容(定义日志格式名字):
log_format hao $remote_addr(日志格式)





nginx日志格式:





2. 定义test.com虚拟主机配置文件(增加访问日志项):
[root@hao-01 vhost]# vim /usr/local/nginx/conf/vhost/test.com.conf
增加内容(访问日志,存储位置,格式名称,分号为结束符号):
access_log /tmp/test.com.log hao;




3. 检测nginx配置文件是否有错?
[root@hao-01 default]# /usr/local/nginx/sbin/nginx -t
4. 重新加载nginx配置文件(非重启!):
[root@hao-01 default]# /usr/local/nginx/sbin/nginx -s reload
5. curl 访问test.com网站地址:
[root@hao-01 ~]# curl -x127.0.0.1:80 test2.com/admin/index.html
6. curl 访问test.com网站地址:
[root@hao-01 ~]# curl -x127.0.0.1:80 test3.com/admin/index.html
7. 查看test.com主机(网站)的访问日志内容:
[root@hao-01 ~]# cat /tmp/test.com.log



12.11 Nginx日志切割
1. 编写一个日志切割shell脚本:
[root@hao-01 ~]# vim /usr/local/sbin/nginx_log_rotate.sh
脚本内容:
#! /bin/bash
d=`date -d "-1 day" +%Y%m%d`
logdir="/tmp/"
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`





2. 执行日志切割shell脚本:
[root@hao-01 ~]# sh -x /usr/local/sbin/nginx_log_rotate.sh
3. 查看/tmp/目录下,匹配含有.log文件(这里指日志文件)
[root@hao-01 ~]# ls /tmp/*.log*





4. 删除(定期清理老日志)/tmp/目录下,匹配大于10天,含有.log-的日志文件: [root@hao-01 ~]# find /tmp/ -name *.log-* -type f -mtime +10 |xargs rm
5. 把切割nginx日志脚本,加入任务计划,设定每天凌晨零点执行一次:
[root@hao-01 ~]# crontab -e






12.12 静态文件不记录日志和过期时间
1. 编辑test.com虚拟主机配置文件(设定日志过滤静态文件和缓存静态文件时间): [root@hao-01 ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
增加内容(日志过滤静态文件和缓存静态文件时间):
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 7d;
access_log off;
}
location ~ .*\.(js|css)$
{
expires 12h;
access_log off;
}





2. 检测nginx配置文件是否有错?
[root@hao-01 default]# /usr/local/nginx/sbin/nginx -t
3. 重新加载nginx配置文件(非重启!):
[root@hao-01 default]# /usr/local/nginx/sbin/nginx -s reload
4. 进入test.com网站目录下:
[root@hao-01 ~]# cd /data/wwwroot/test.com/
5. 创建一些以.jpg .png .js格式的静态文件:
[root@hao-01 test.com]# touch /data/wwwroot/test.com/1.jpg 2.png 3.js





6. curl访问 test.com网站下1.jpg静态格式文件:
[root@hao-01 test.com]# curl -x127.0.0.1:80 test.com/1.jpg -I



7. 查看test.com网站的访问日志,看看是否有记录静态格式文件访问??? [root@hao-01 test.com]# cat /tmp/test.com.log
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  nginx日志切割
相关文章推荐