2018-3-14 12周3次课 Nginx访问日志日志分割日志不记录静态文件和过期时间
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2018-3-14 12周3次课 Nginx访问日志日志分割日志不记录静态文件和过期时间相关的知识,希望对你有一定的参考价值。
12.10 nginx访问日志
·日志格式:
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
搜索log_format
(虽然红框中有三行,但实际上时一行配置,以分号为结尾)
combined_realip 定义日志格式名字,此处定义成什么,那么后面引用时就要写成什么
公网ip(出口ip)
·除了在主配置文件nginx.conf里定义日志格式外,还需要在虚拟主机配置文件中增加
access_log /tmp/1.log combined_realip
(combined_realip就是nginx.conf中的日志格式名)
[[email protected] vhost]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [[email protected] vhost]# /usr/local/nginx/sbin/nginx -s reload [[email protected] vhost]# curl -x127.0.0.1:80 test3.com/admin/index.html -I HTTP/1.1 404 Not Found Server: nginx/1.12.2 Date: Wed, 14 Mar 2018 13:35:48 GMT Content-Type: text/html Content-Length: 169 Connection: keep-alive [[email protected] vhost]# curl -x127.0.0.1:80 test2.com/admin/index.html -I HTTP/1.1 301 Moved Permanently Server: nginx/1.12.2 Date: Wed, 14 Mar 2018 13:35:56 GMT Content-Type: text/html Content-Length: 185 Connection: keep-alive Location: [[email protected] vhost]# curl -x127.0.0.1:80 test.com/admin/index.html -I HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Wed, 14 Mar 2018 13:36:21 GMT Content-Type: text/html Content-Length: 19 Last-Modified: Tue, 13 Mar 2018 15:39:53 GMT Connection: keep-alive ETag: "5aa7f0c9-13" Accept-Ranges: bytes [[email protected] vhost]# cat /tmp/test.com.log 127.0.0.1 - [14/Mar/2018:21:35:56 +0800] test2.com "/admin/index.html" 301 "-" "curl/7.29.0" 127.0.0.1 - [14/Mar/2018:21:36:21 +0800] test.com "/admin/index.html" 200 "-" "curl/7.29.0"
12.11 Nginx日志切割
创建shell脚本:
[[email protected] vhost]# vim /usr/local/sbin/nginx_log_rotate.sh
d=`date -d "-1 day" +%Y%m%d` 为了生成昨天的日期
logdir="/tmp/" 存放日志的目录
nginx_pid="/usr/local/nginx/logs/nginx.pid" 找pid为了重新加载以便重新写新的日志(日志pid)
cd $logdir 进入到日志文件夹
for log in `ls *.log` 在运行目录logdir下都有哪些文件,每个文件作为一次循环的对象
do
mv $log $log-$d 所有log改名字,以昨天的日期为后缀
/bin/kill -HUP `cat $nginx_pid` 重新加载,生成新的test.com.log
[[email protected] vhost]# sh -x /usr/local/sbin/nginx_log_rotate.sh ##sh -x 运行可查看过程 ++ date -d '-1 day' +%Y%m%d + d=20180313 + logdir=/tmp/ + nginx_pid=/usr/local/nginx/logs/nginx.pid + cd /tmp/ ++ ls test.com.log + for log in '`ls *.log`' + mv test.com.log test.com.log-20180313 ++ cat /usr/local/nginx/logs/nginx.pid + /bin/kill -HUP 823
一段时间后删除早前的log文件
find /tmp/ -name *.log-* -type f -mtime +30 | xarge rm
任务计划:(每天凌晨零点开始执行)
[[email protected] vhost]# crontab -e
0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh
12.12 静态文件不记录日志和过期时间
location ~ 精准匹配 任意一个以 .gif或jpg或jpeg或png或bmp或swf 为结尾的文件
配置过期时间,不去记录访问日志
由于配置过期时间不同,因此分开写上下两段,js|css和上面分开
创建1.gif和2.js文件,模拟虚拟服务器中有这两种格式文件,我们来访问他们,在访问同一个虚拟主机中的index.html文件,查看log文件,可以看出,log不会记录指定格式的静态文件
[[email protected] vhost]# cd /data/wwwroot/test.com/ [[email protected] test.com]# vim 1.gif [[email protected] test.com]# vim 2.js [[email protected] test.com]# curl -x127.0.0.1:80 test.com/1.gif ifjasdfajsdfladskf [[email protected] test.com]# curl -x127.0.0.1:80 test.com/2.js dlkfjad;slkfja;lk [[email protected] test.com]# curl -x127.0.0.1:80 test.com/index.html test.com [[email protected] test.com]# cat /tmp/test.com.log 127.0.0.1 - [14/Mar/2018:22:33:25 +0800] test.com "/index.html" 200 "-" "curl/7.29.0" [[email protected] test.com]# curl -x127.0.0.1:80 test.com/index.html test.com [[email protected] test.com]# cat /tmp/test.com.log 127.0.0.1 - [14/Mar/2018:22:33:25 +0800] test.com "/index.html" 200 "-" "curl/7.29.0" 127.0.0.1 - [14/Mar/2018:22:33:36 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
如果js后面跟一些其他字符,就无法匹配规则,因此会记录
[[email protected] test.com]# curl -x127.0.0.1:80 test.com/2.jsdafafa <html> <head><title>404 Not Found</title></head> <body bgcolor="white"> <center><h1>404 Not Found</h1></center> <hr><center>nginx/1.12.2</center> </body> </html> [[email protected] test.com]# cat /tmp/test.com.log 127.0.0.1 - [14/Mar/2018:22:33:25 +0800] test.com "/index.html" 200 "-" "curl/7.29.0" 127.0.0.1 - [14/Mar/2018:22:33:36 +0800] test.com "/index.html" 200 "-" "curl/7.29.0" 127.0.0.1 - [14/Mar/2018:22:36:25 +0800] test.com "/2.jsdafafa" 404 "-" "curl/7.29.0"
过期时间:
如果去掉配置文件中的 expires,那么不会有过期时间
[[email protected] test.com]# vim /usr/local/nginx/conf/vhost/test.com.conf [[email protected] test.com]# /usr/local/nginx/sbin/nginx -s reload [[email protected] test.com]# curl -x127.0.0.1:80 test.com/2.js -I
以上是关于2018-3-14 12周3次课 Nginx访问日志日志分割日志不记录静态文件和过期时间的主要内容,如果未能解决你的问题,请参考以下文章