tab ane pac 12 月 计划 asdasd sans min ref
一、nginx 访问日志[root@lnmp ~]# vim /usr/local/nginx/conf/nginx.conf (修改 Nginx 的配置文件)
搜索 / log_format (log_format 后面跟的 combined_realip 是一个自定义名字,用来定义整个日志格式,这里写什么,虚拟配置文件后面就可以加上什么,我这里将 combined_realip 修改为 lty)
log_format lty '$remote_addr $http_x_forwarded_for [$time_local]'
'$host"$request_uri"$status'
'"$http_referer" "$http_user_agent"';
[root@lnmp ~]# vim /usr/local/nginx/conf/vhost/test.com.conf (修改虚拟机文件)
access_log /tmp/test.com.log lty; (增加一行,lty 是刚在主配置文件里写的日志格式)
检查语法错误并且重新加载配置文件:
[root@lnmp ~]# /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@lnmp ~]# /usr/local/nginx/sbin/nginx -s reload
检测;
[root@lnmp ~]# curl -x127.0.0.1:80 test2.com/admin/1.php -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.8.0
Date: Thu, 14 Dec 2017 05:32:49 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://test.com/admin/1.php
[root@lnmp ~]# curl -x127.0.0.1:80 test.com/admin/1.php
touch file.php
[root@lnmp ~]# cat /tmp/test.com.log (查看日志)
127.0.0.1 - [14/Dec/2017:13:32:49 +0800] test2.com "/admin/1.php" 301 "-" "curl/7.29.0"
127.0.0.1 - [14/Dec/2017:13:33:10 +0800] test.com "/admin/1.php" 200 "-" "curl/7.29.0"
二、nginx 的日志切割nginx 日志切割
nginx 没有像 httpd 一样,自己带有切割工具,则需要借助系统的切割工具或者自己写一个切割的脚本
[root@lnmp ~]# vim /usr/local/sbin/nginx_log_rotate.sh (写一个自主切割的脚本)
#!/bin/bash
#假设 nginx 的日志存放路径为 / data/logs/
d=`date -d "-1 day" +%Y%m%d` (生成一个年月日 day -1 的日期,(昨天的日期))
logdir="/tmp/" (定义 logdir 为 / tmp)
nginx_pid="/usr/local/nginx/logs/nginx.pid" (给 Nginx.pid 定义一个变量,为下面命令做准备)
cd $logdir (进入到 logdir 中)
for log in `ls *.log` (做一个 for 循环,ls 当前目录下所有以. log 文件为结尾的文件)
do
mv $log $log-$d (把以 log 为结尾的日志名都改成 log--- 日期)
done
/bin/kill -HUP `cat $nginx_pid` (重新启动 nginx_pid 进程,重新生成一个 test.com.log 文件)
执行脚本:
[root@lnmp ~]# sh -x /usr/local/sbin/nginx_log_rotate.sh
++ date -d '-1 day' +%Y%m%d
+ d=20171213
+ 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-20171213
++ cat /usr/local/nginx/logs/nginx.pid
+ /bin/kill -HUP 1157
[root@lnmp ~]# ll /tmp/
srwxrwxrwx. 1 mysql mysql 0 12 月 14 11:56 mysql.sock
srw-rw-rw-. 1 root root 0 12 月 14 11:55 php-fcgi.sock
drwx------. 3 root root 17 12 月 14 11:55 systemd-private-50670dd070a94a6f85f2f82feb779c46-vmtoolsd.service-vZTOZz
-rw-r--r--. 1 root root 0 12 月 14 13:54 test.com.log
-rw-r--r--. 1 root root 0 12 月 14 13:54 test.com.log-20171213
最后一步,添加任务计划:
[root@lnmp ~]# crontab -e
no crontab for root - using an empty one
0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh (添加一行)
三、静态文件不记录日志[root@lnmp ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ (以 gif,jpg,jpeg,png,bmp,swf 结尾的文件保存 7 天,并且不记录日志)
{
expires 7d;
access_log off;
}
location ~ .*\.(js|CSS)$
{
expires 12h; (以 js,css 结尾的文件保存 12 小时,并且不记录日志)
access_log off;
}
检查语法并且重新加载配置文件:
[root@lnmp ~]# /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@lnmp ~]# /usr/local/nginx/sbin/nginx -s reload
证明:分别访问了以 gif,js,html 为结尾的 3 个文件,发现日志里只记录了 html 为结尾的访问信息。
[root@lnmp ~]# curl -x127.0.0.1:80 test.com/1.gif
dasdasdafasdfaf
[root@lnmp ~]# curl -x127.0.0.1:80 test.com/2.js
fdasfsadfasdfzczv
[root@lnmp ~]# curl -x127.0.0.1:80 test.com/index.html
test.com
[root@lnmp ~]# cat /tmp/test.com.log
127.0.0.1 - [14/Dec/2017:14:30:40 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
LNMP 架构(nginx 访问日志,Nginx 日志切割,静态文件不记录访问日志)
来源: http://www.bubuko.com/infodetail-2451370.html