快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

12.10Nginx访问日志

12.10 Nginx访问日志

查看/usr/local/nginx/conf/nginx.conf的日志格式:

成都创新互联专业为企业提供加查网站建设、加查做网站、加查网站设计、加查网站制作等企业网站建设、网页设计与制作、加查企业网站模板建站服务,10余年加查做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

[root@DasonCheng ~]# grep -A2 log_format /usr/local/nginx/conf/nginx.conf log_format custom \'$remote_addr $http_x_forwarded_for [$time_local]\' \' $host "$request_uri" $status\' \' "$http_referer" "$http_user_agent"\'; //Nginx里面";"才表示换行

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

access_log /tmp/1.log custom; //这里的custom就是在nginx.conf中定义的日志格式名字 -t && -s reload curl -x127.0.0.1:80 test.com -I cat /tmp/1.log

测试:

[root@DasonCheng tmp]# vim /usr/local/nginx/conf/vhost/test.com.conf access_log /tmp/1.log custom; //添加这一行配置; …… [root@DasonCheng tmp]# /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@DasonCheng tmp]# /usr/local/nginx/sbin/nginx -s reload …… [root@DasonCheng tmp]# curl -x127.0.0.1:80 test.com/admin/1.php File not found. [root@DasonCheng tmp]# curl -x127.0.0.1:80 test.com/admin/index.html directory test ! [root@DasonCheng tmp]# ll 总用量 8 -rw-r--r--. 1 root root 179 8月 16 18:11 1.log srwxrwxrwx. 1 mysql mysql 0 8月 16 15:24 mysql.sock drwxr-xr-x. 3 root root 18 8月 10 12:03 pear srw-rw-rw-. 1 root root 0 8月 16 15:24 php-fcgi.sock -rw-r--r--. 1 root root 172 8月 16 18:04 ssl.log drwx------. 3 root root 17 8月 16 15:24 systemd-private-b71fa4297-vmtoolsd.service-beD38N [root@DasonCheng tmp]# cat 1.log 127.0.0.1 - [16/Aug/2017:18:10:52 +0800] test.com "/admin/1.php" 404 "-" "curl/7.29.0" 127.0.0.1 - [16/Aug/2017:18:11:01 +0800] test.com "/admin/index.html" 200 "-" "curl/7.29.0" [root@DasonCheng tmp]# 12.11 Nginx日志切割

自定义shell 脚本 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` 任务计划 0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh 脚本解析:

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" //为了执行下面的命令,kill cd $logdir //进入日志目录; for log in `ls *.log` //做循环for,log作为其变量,log为`ls *.log`的变量;执行下面操作: do mv $log $log-$d //重命名 done /bin/kill -HUP `cat $nginx_pid` //重新加载,生成新的.log

执行结果:

[root@DasonCheng tmp]# cat /usr/local/sbin/nginx_logrotate.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`

[root@DasonCheng tmp]# sh -x /usr/local/sbin/nginx_logrotate.sh ++ date -d \'-1 day\' +%Y%m%d + d=20170815 + logdir=/tmp/ + nginx_pid=/usr/local/nginx/logs/nginx.pid + cd /tmp/ ++ ls 1.log ssl.log + for log in \'`ls *.log`\' + mv 1.log 1.log-20170815 + for log in \'`ls *.log`\' + mv ssl.log ssl.log-20170815 ++ cat /usr/local/nginx/logs/nginx.pid + /bin/kill -HUP 2621 [root@DasonCheng tmp]# ls 1.log php-fcgi.sock 1.log-20170815 ssl.log …… 清理日志:

[root@DasonCheng ~]# find /tmp/ -name *.log-* -type f -mtime +30 | xargs rm //删除三十天以前的文件! rm: 缺少操作数 Try \'rm --help\' for more information. 制定定时计划任务:

[root@DasonCheng ~]# crontab -e 0 0 * * * /bin/bash /usr/local/sbin/nginx_logrotate.sh 12.12 静态文件不记录日志和过期时间

配置如下 location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ { expires 7d; access_log off; } location ~ .*.(js|css)$ { expires 12h; access_log off; } 配置解析:

[root@DasonCheng ~]# vim /usr/local/nginx/conf/vhost/test.com.conf location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ //相当于匹配url中的静态文件,Nginx正则表达式进行匹配; { expires 7d; //其实可以把这两个写在一起,但expires过期时间不一致,所以没有写在一起! access_log off; } location ~ .*.(js|css)$ { expires 12h; access_log off; } 测试结果:

[root@DasonCheng ~]# /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@DasonCheng ~]# /usr/local/nginx/sbin/nginx -s reload [root@DasonCheng ~]# vim /data/wwwroot/test.com/1.gif [root@DasonCheng ~]# vim /data/wwwroot/test.com/2.js

[root@DasonCheng ~]# curl -x127.0.0.1:80 test.com/index.html authorization ok ! [root@DasonCheng ~]# curl -x127.0.0.1:80 test.com/1.gif dfsalkdjf;aslkdjf [root@DasonCheng ~]# curl -x127.0.0.1:80 test.com/2.js sadf;lkjsdfa [root@DasonCheng ~]# tail /tmp/1.log 127.0.0.1 - [16/Aug/2017:19:06:39 +0800] test.com "/index.html" 200 "-" "curl/7.29.0" //日志只记录了index.html这个页面记录; [root@DasonCheng ~]# curl -x127.0.0.1:80 test.com/2.js -i HTTP/1.1 200 OK Server: nginx/1.12.1 Date: Wed, 16 Aug 2017 11:07:27 GMT Content-Type: application/javascript Content-Length: 13 Last-Modified: Wed, 16 Aug 2017 11:03:54 GMT Connection: keep-alive ETag: "5994269a-d" Expires: Wed, 16 Aug 2017 23:07:27 GMT Cache-Control: max-age=43200 //没有配置expires的话是没有这个过期时间的哦! Accept-Ranges: bytes sadf;lkjsdfa


分享文章:12.10Nginx访问日志
文章地址:http://6mz.cn/article/cpjcgj.html

其他资讯