12.10-12.12 Nginx的訪問日誌,日誌切割,靜態文件不記錄日誌和過期時間

12.10 Nginx訪問日誌

12.11 Nginx日誌切割

12.12 靜態文件不記錄日誌和過期時間



12.10 Nginx訪問日誌


1 打開配置文件,搜索/log_format,查看日誌文件格式。或者直接grep過濾出來

[root@AliKvn vhost]# grep -A2 log_format /usr/local/nginx/conf/nginx.conf

   log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
    ' $host "$request_uri" $status'
    ' "$http_referer" "$http_user_agent"';

blob.png

2 其中combined_realip是日誌格式名稱,可以自定義。

#vim nginx.cnf 

blob.png

blob.png

blob.png

3 除了在主配置文件nginx.conf裏定義日誌格式外,還需要在虛擬主機配置文件中增加

[root@AliKvn vhost]# vim test.com.conf 

 access_log /tmp/test.com.log aming;

4 -t  && -s reload 檢查並重新加載

[root@AliKvn vhost]# /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@AliKvn vhost]# /usr/local/nginx/sbin/nginx -s reload

5 curl測試

blob.png

6 訪問日誌

blob.png



12.11 Nginx日誌切割

1 自定義shell 腳本

 vim /usr/local/sbin/nginx_log_rotate.sh //寫入如下內容

shell腳本格式

#! /bin/bash
## 假設nginx的日誌存放路徑爲/data/logs/
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`


參數解析

d=`date -d "-1 day" +%Y%m%d`  

這裏的格式是現在的時間,減去1天,意思幾時今天的昨天。

日誌一般是第二天凌晨0:00生成的,所以切割日誌,必然是昨天的時間。

%Y%m%d` 是時間的格式,表示年,月,日。

logdir="/tmp/"

日誌所在路徑,此處應該是/tmp/,因爲test.com.log在tmp/

nginx_pid="/usr/local/nginx/logs/nginx.pid"

列出nginx_pid路徑目的是配合執行-HUP `cat $nginx_pid`變量使用,

/bin/kill -HUP `cat $nginx_pid`相當於nginx -s reload重新加載功能。

cd $logdir
for log in `ls *.log`
do
    mv $log $log-$d
done

(cd $logdir)進入$logdir(/tmp/),

然後ls看看都有哪些.log(*.log)文件,把所有log的文件改名字,

名字格式是,昨天的日期加後綴.log(mv $log $log-$d),

參考for的格式:

for f in `ls `; do ls -l $f; done

   文件 在哪 ls序列 `;  

[root@AliKvn vhost]# for f in `ls `; do ls -l $f; done

-rw-r--r-- 1 root root 142 Apr 24 16:58 aaa.com.conf

-rw-r--r-- 1 root root 419 Apr 25 16:18 test.com.conf


2 執行腳本

[root@AliKvn vhost]# vim  /usr/local/sbin/nginx_log_rotate.sh

[root@AliKvn tmp]# sh -x  /usr/local/sbin/nginx_log_rotate.sh

++ date -d '-1 day' +%Y%m%d

+ d=20180424

+ 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-20180424

++ cat /usr/local/nginx/logs/nginx.pid

+ /bin/kill -HUP 17745


3 清理30天前的日誌(這個看實際環境而操作,測試的話就無須做計劃任務)

find /tmp/ -name *.log-* -type f -mtime +30 |xargs rm  



4 任務計劃,讓其每天凌晨0:00去執行這個腳本(這個看實際環境而操作,測試的話就無須做計劃任務)

#crontab -e

 0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh


12.12 靜態文件不記錄日誌和過期時間

大綱

blob.png

1 配置如下

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
          expires      7d;
          access_log off;
    }
location ~ .*\.(js|css)$
    {
          expires      12h;
          access_log off;
    }


參數解析,

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

location ~匹配請求,後面帶指定對應的靜態文件。

\.(gif|jpg|jpeg|png|bmp|swf)$ 

\脫義,\.後綴,()裏面是文件類型,|表示或者。

2 -t && -s reload 檢查語法並重新加載

[root@AliKvn vhost]# /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@AliKvn vhost]# /usr/local/nginx/sbin/nginx -s reload

3 curl測試

準備工作:

touch 1.gif 和 2.js 

分別鍵入任意內容1111 222

測試

[root@AliKvn test.com]# curl -x127.0.0.1:80 test.com/1.gif

111111111111111111111111111111111

[root@AliKvn test.com]# curl -x127.0.0.1:80 test.com/2.js

2222

[root@AliKvn test.com]# curl -x127.0.0.1:80 test.com/index.html

test.com

4 訪問日誌

[root@AliKvn test.com]# cat /tmp/test.com.log

127.0.0.1 - [25/Apr/2018:17:56:32 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"

再次訪問不存在的頁面,404頁面

[root@AliKvn test.com]# curl -x127.0.0.1:80 test.com/2.js11111111

<html>

<head><title>404 Not Found</title></head>

<body bgcolor="white">

<center><h1>404 Not Found</h1></center>

<hr><center>nginx/1.8.0</center>

</body>

</html>

[root@AliKvn test.com]# !cat

cat /tmp/test.com.log

127.0.0.1 - [25/Apr/2018:17:56:32 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"

127.0.0.1 - [25/Apr/2018:18:01:04 +0800] test.com "/2.js11111111" 404 "-" "curl/7.29.0"

訪問日誌記錄可以看到,curl 1.gif與2.js的信息被過濾掉,

而其他沒被標註的靜態元素文件卻被記錄這訪問記錄。

5 測試過期時間

blob.png

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章