12.10 Nginx訪問日誌

12.10 Nginx訪問日誌

mark
查看/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]#

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

執行結果: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

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
相關文章
相關標籤/搜索