Nginx访问日志、日志切割、静态文件不记录日志和过期时间

时间:2022-02-27 19:32:13

6月8日任务

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

 

12.10 Nginx访问日志

Nginx访问日志、日志切割、静态文件不记录日志和过期时间

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

 [root@jimmylinux-001 vhost]# vim test.com.conf

Nginx访问日志、日志切割、静态文件不记录日志和过期时间

重新加载配置文件后测试

[root@jimmylinux-001 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@jimmylinux-001 vhost]# /usr/local/nginx/sbin/nginx -s reload

Nginx访问日志、日志切割、静态文件不记录日志和过期时间

Nginx访问日志、日志切割、静态文件不记录日志和过期时间

 

12.11 Nginx日志切割

Nginx不像Apache有自带的日志切割工具,所以需要借助于系统的日志切割工具或者自己写日志切割脚本。

1、创建一个shell脚本

[root@jimmylinux-001 vhost]# 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" #! /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`
[root@jimmylinux-001 vhost]# sh -x /usr/local/sbin/nginx_log_rotate.sh  sh运行脚本,-x表示查看执行脚本的过程。 ++ date -d '-1 day' +%Y%m%d + d=20180607
+ 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-20180607
++ cat /usr/local/nginx/logs/nginx.pid + /bin/kill -HUP 1049

以上就是日志切割的脚本,长此以往每天都会生成一个日志,除了每天切割之外还需要做一个清理,可以执行如下命令清理。

[root@jimmylinux-001 vhost]# find /tmp/ -name *.log-* -type f -mtime +30 |xargs rm rm: 缺少操作数 因为没有满足条件的文件,所以会报错提示“缺少操作数” Try 'rm --help' for more information.

2、加一个任务计划

[root@jimmylinux-001 vhost]# crontab -e 加一个任务计划 0 0 * * * /usr/local/sbin/nginx_log_rotate.sh  每天的凌晨0点开始执行

Nginx访问日志、日志切割、静态文件不记录日志和过期时间

 

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

在Apache配置的时候介绍了静态文件可以设置不记录日志的,那么在Nginx里面同样也可以把一些静态文件忽略掉,不记录日志。

1、编辑配置文件test.com.conf

[root@jimmylinux-001 vhost]# vim test.com.conf 添加以下内容 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$  匹配URL里面的关键词,括号里面的|是或者,.\是脱义的意思。 { expires 7d; 过期时间7天 access_log off; } location ~ .*\.(js|css)$ { expires 12h; access_log off; }

2、重新加载配置文件后模拟一个图片

[root@jimmylinux-001 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@jimmylinux-001 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@jimmylinux-001 vhost]# cd /data/wwwroot/test.com/ [root@jimmylinux-001 test.com]# ls admin index.html [root@jimmylinux-001 test.com]# vim 1.gif 新建一个1.gif文件 [root@jimmylinux-001 test.com]# vim 2.js 新建一个2.js文件 [root@jimmylinux-001 test.com]# curl -x127.0.0.1:80 test.com/1.jif <html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html> [root@jimmylinux-001 test.com]# curl -x127.0.0.1:80 test.com/1.gif sdjjfdsjfl [root@jimmylinux-001 test.com]# curl -x127.0.0.1:80 test.com/2.js yyyyybbbbbaaaaccccc [root@jimmylinux-001 test.com]# curl -x127.0.0.1:80 test.com/index.html test.com [root@jimmylinux-001 test.com]# cat /tmp/test.com.log 127.0.0.1 - [08/Jun/2018:23:17:04 +0800] test.com "/1.jif" 404 "-" "curl/7.29.0"
127.0.0.1 - [08/Jun/2018:23:19:02 +0800] test.com "/index.html" 200 "-" "curl/7.29.0" [root@jimmylinux-001 test.com]# curl -x127.0.0.1:80 test.com/index.html test.com [root@jimmylinux-001 test.com]# cat /tmp/test.com.log 127.0.0.1 - [08/Jun/2018:23:17:04 +0800] test.com "/1.jif" 404 "-" "curl/7.29.0"
127.0.0.1 - [08/Jun/2018:23:19:02 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [08/Jun/2018:23:19:37 +0800] test.com "/index.html" 200 "-" "curl/7.29.0" [root@jimmylinux-001 test.com]# curl -x127.0.0.1:80 test.com/2.js yyyyybbbbbaaaaccccc [root@jimmylinux-001 test.com]# cat /tmp/test.com.log 127.0.0.1 - [08/Jun/2018:23:17:04 +0800] test.com "/1.jif" 404 "-" "curl/7.29.0"
127.0.0.1 - [08/Jun/2018:23:19:02 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [08/Jun/2018:23:19:37 +0800] test.com "/index.html" 200 "-" "curl/7.29.0" [root@jimmylinux-001 test.com]# curl -x127.0.0.1:80 test.com/2.jsfsdfsd <html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html> 
[root@jimmylinux
-001 test.com]# cat /tmp/test.com.log 127.0.0.1 - [08/Jun/2018:23:17:04 +0800] test.com "/1.jif" 404 "-" "curl/7.29.0" 127.0.0.1 - [08/Jun/2018:23:19:02 +0800] test.com "/index.html" 200 "-" "curl/7.29.0" 127.0.0.1 - [08/Jun/2018:23:19:37 +0800] test.com "/index.html" 200 "-" "curl/7.29.0" 127.0.0.1 - [08/Jun/2018:23:20:59 +0800] test.com "/2.jsfsdfsd" 404 "-" "curl/7.29.0"
[root@jimmylinux
-001 test.com]# curl -x127.0.0.1:80 -I test.com/2.js HTTP/1.1 200 OK Server: nginx/1.12.1 Date: Fri, 08 Jun 2018 15:21:44 GMT Content-Type: application/javascript Content-Length: 20 Last-Modified: Fri, 08 Jun 2018 15:16:37 GMT Connection: keep-alive ETag: "5b1a9dd5-14" Expires: Sat, 09 Jun 2018 03:21:44 GMT 过期时间 Cache-Control: max-age=43200 过期最大值 Accept-Ranges: bytes [root@jimmylinux-001 test.com]# vim /usr/local/nginx/conf/vhost/test.com.conf 编辑配置文件,把过期时间加#注释掉。
[root@jimmylinux
-001 test.com]# /usr/local/nginx/sbin/nginx -s reload 重新加载配置文件 [root@jimmylinux-001 test.com]# curl -x127.0.0.1:80 -I test.com/2.js 重新再访问 HTTP/1.1 200 OK Server: nginx/1.12.1 Date: Fri, 08 Jun 2018 15:26:34 GMT Content-Type: application/javascript Content-Length: 20 Last-Modified: Fri, 08 Jun 2018 15:16:37 GMT Connection: keep-alive ETag: "5b1a9dd5-14" 这个时候就没有过期时间了 Accept-Ranges: bytes

Nginx访问日志、日志切割、静态文件不记录日志和过期时间

Nginx访问日志、日志切割、静态文件不记录日志和过期时间

Nginx访问日志、日志切割、静态文件不记录日志和过期时间

Nginx访问日志、日志切割、静态文件不记录日志和过期时间