查看/usr/local/nginx/conf/nginx.conf的日志格式:javascript
[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里定义日志格式外,还须要在虚拟主机配置文件中增长:php
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
测试:css
[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]#
自定义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
执行结果:html
[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
配置以下 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