查看/usr/local/nginx/conf/nginx.conf的日志格式:
成都創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供加查網(wǎng)站建設(shè)、加查做網(wǎng)站、加查網(wǎng)站設(shè)計(jì)、加查網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、加查企業(yè)網(wǎng)站模板建站服務(wù),10余年加查做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。[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里定義日志格式外,還需要在虛擬主機(jī)配置文件中增加:
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
測(cè)試:
[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//寫入如下內(nèi)容 #! /bin/bash ## 假設(shè)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` 任務(wù)計(jì)劃 0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh 腳本解析:
vim /usr/local/sbin/nginx_log_rotate.sh//寫入如下內(nèi)容 #! /bin/bash d=`date -d "-1 day" +%Y%m%d` //可以直接輸入,這個(gè)日期是昨天的; logdir="/tmp/" //日志目錄; nginx_pid="/usr/local/nginx/logs/nginx.pid" //為了執(zhí)行下面的命令,kill cd $logdir //進(jìn)入日志目錄; for log in `ls *.log` //做循環(huán)for,log作為其變量,log為`ls *.log`的變量;執(zhí)行下面操作: do mv $log $log-$d //重命名 done /bin/kill -HUP `cat $nginx_pid` //重新加載,生成新的.log
執(zhí)行結(jié)果:
[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: 缺少操作數(shù) Try \'rm --help\' for more information. 制定定時(shí)計(jì)劃任務(wù):
[root@DasonCheng ~]# crontab -e 0 0 * * * /bin/bash /usr/local/sbin/nginx_logrotate.sh 12.12 靜態(tài)文件不記錄日志和過(guò)期時(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)$ //相當(dāng)于匹配url中的靜態(tài)文件,Nginx正則表達(dá)式進(jìn)行匹配; { expires 7d; //其實(shí)可以把這兩個(gè)寫在一起,但expires過(guò)期時(shí)間不一致,所以沒(méi)有寫在一起! access_log off; } location ~ .*.(js|css)$ { expires 12h; access_log off; } 測(cè)試結(jié)果:
[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這個(gè)頁(yè)面記錄; [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 //沒(méi)有配置expires的話是沒(méi)有這個(gè)過(guò)期時(shí)間的哦! Accept-Ranges: bytes sadf;lkjsdfa