标签:Nginx
12.10 Nginx访问日志·日志格式:
[root@localhost ~]# 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中的日志格式名)
[root@localhost 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 [root@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload [root@localhost 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 [root@localhost 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: [root@localhost 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 [root@localhost 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"
创建shell脚本:
[root@localhost 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
[root@localhost 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
任务计划:(每天凌晨零点开始执行)
[root@localhost 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不会记录指定格式的静态文件
[root@localhost vhost]# cd /data/wwwroot/test.com/ [root@localhost test.com]# vim 1.gif [root@localhost test.com]# vim 2.js [root@localhost test.com]# curl -x127.0.0.1:80 test.com/1.gif ifjasdfajsdfladskf [root@localhost test.com]# curl -x127.0.0.1:80 test.com/2.js dlkfjad;slkfja;lk [root@localhost test.com]# curl -x127.0.0.1:80 test.com/index.html test.com [root@localhost 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" [root@localhost test.com]# curl -x127.0.0.1:80 test.com/index.html test.com [root@localhost 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后面跟一些其他字符,就无法匹配规则,因此会记录
[root@localhost 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> [root@localhost 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,那么不会有过期时间
[root@localhost test.com]# vim /usr/local/nginx/conf/vhost/test.com.conf [root@localhost test.com]# /usr/local/nginx/sbin/nginx -s reload [root@localhost test.com]# curl -x127.0.0.1:80 test.com/2.js -I
2018-3-14 12周3次课 Nginx访问日志、日志分割、日志不记录静态文件和过期时间
标签:Nginx
原文地址:http://blog.51cto.com/11530642/2086980