Nginx

12.10 Nginx訪問日誌


·日誌格式:

[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf

搜索log_format

技術分享圖片

(雖然紅框中有三行,但實際上時一行配置,以分號為結尾)


combined_realip 定義日誌格式名字,此處定義成什麽,那麽後面引用時就要寫成什麽

技術分享圖片

技術分享圖片


公網ip(出口ip)

技術分享圖片


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

access_log /tmp/1.log combined_realip

(combined_realip就是nginx.conf中的日誌格式名)

技術分享圖片

[[email protected] 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
[[email protected] vhost]# /usr/local/nginx/sbin/nginx -s reload
[[email protected] vhost]# curl -x127.0.0.1:80 test3.com/admin/index.html -I
HTTP/1.1 404 Not Found
Server: nginx/1.12.2
Date: Wed, 14 Mar 2018 13:35:48 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive

[[email protected] vhost]# curl -x127.0.0.1:80 test2.com/admin/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.2
Date: Wed, 14 Mar 2018 13:35:56 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location:  

[[email protected] vhost]# curl -x127.0.0.1:80 test.com/admin/index.html -I
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Wed, 14 Mar 2018 13:36:21 GMT
Content-Type: text/html
Content-Length: 19
Last-Modified: Tue, 13 Mar 2018 15:39:53 GMT
Connection: keep-alive
ETag: "5aa7f0c9-13"
Accept-Ranges: bytes

[[email protected] vhost]# cat /tmp/test.com.log
127.0.0.1 - [14/Mar/2018:21:35:56 +0800] test2.com "/admin/index.html" 301 "-" "curl/7.29.0"
127.0.0.1 - [14/Mar/2018:21:36:21 +0800] test.com "/admin/index.html" 200 "-" "curl/7.29.0"





12.11 Nginx日誌切割


創建shell腳本:

[[email protected] vhost]# vim /usr/local/sbin/nginx_log_rotate.sh

技術分享圖片


d=`date -d "-1 day" +%Y%m%d` 為了生成昨天的日期

logdir="/tmp/" 存放日誌的目錄

nginx_pid="/usr/local/nginx/logs/nginx.pid" 找pid為了重新加載以便重新寫新的日誌(日誌pid)

cd $logdir 進入到日誌文件夾

for log in `ls *.log` 在運行目錄logdir下都有哪些文件,每個文件作為一次循環的對象

do

mv $log $log-$d 所有log改名字,以昨天的日期為後綴

/bin/kill -HUP `cat $nginx_pid` 重新加載,生成新的test.com.log


[[email protected] vhost]# sh -x /usr/local/sbin/nginx_log_rotate.sh    ##sh -x 運行可查看過程
++ date -d '-1 day' +%Y%m%d
+ d=20180313
+ 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-20180313
++ cat /usr/local/nginx/logs/nginx.pid
+ /bin/kill -HUP 823

技術分享圖片

一段時間後刪除早前的log文件

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


任務計劃:(每天淩晨零點開始執行)

[[email protected] vhost]# crontab -e

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





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


技術分享圖片

location ~ 精準匹配 任意一個以 .gif或jpg或jpeg或png或bmp或swf 為結尾的文件

配置過期時間,不去記錄訪問日誌

由於配置過期時間不同,因此分開寫上下兩段,js|css和上面分開


創建1.gif和2.js文件,模擬虛擬服務器中有這兩種格式文件,我們來訪問他們,在訪問同一個虛擬主機中的index.html文件,查看log文件,可以看出,log不會記錄指定格式的靜態文件

[[email protected] vhost]# cd /data/wwwroot/test.com/
[[email protected] test.com]# vim 1.gif
[[email protected] test.com]# vim 2.js
[[email protected] test.com]# curl -x127.0.0.1:80 test.com/1.gif
ifjasdfajsdfladskf
[[email protected] test.com]# curl -x127.0.0.1:80 test.com/2.js
dlkfjad;slkfja;lk
[[email protected] test.com]# curl -x127.0.0.1:80 test.com/index.html
test.com
[[email protected] test.com]# cat /tmp/test.com.log
127.0.0.1 - [14/Mar/2018:22:33:25 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
[[email protected] test.com]# curl -x127.0.0.1:80 test.com/index.html
test.com
[[email protected] test.com]# cat /tmp/test.com.log
127.0.0.1 - [14/Mar/2018:22:33:25 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [14/Mar/2018:22:33:36 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"

如果js後面跟一些其他字符,就無法匹配規則,因此會記錄

[[email protected] test.com]# curl -x127.0.0.1:80 test.com/2.jsdafafa
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.12.2</center>
</body>
</html>
[[email protected] test.com]# cat /tmp/test.com.log
127.0.0.1 - [14/Mar/2018:22:33:25 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [14/Mar/2018:22:33:36 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [14/Mar/2018:22:36:25 +0800] test.com "/2.jsdafafa" 404 "-" "curl/7.29.0"


過期時間:

技術分享圖片

如果去掉配置文件中的 expires,那麽不會有過期時間

技術分享圖片

[[email protected] test.com]# vim /usr/local/nginx/conf/vhost/test.com.conf
[[email protected] test.com]# /usr/local/nginx/sbin/nginx -s reload
[[email protected] test.com]# curl -x127.0.0.1:80 test.com/2.js -I

技術分享圖片技術分享圖片





2018-3-14 12周3次課 Nginx訪問日誌、日誌分割、日誌不記錄靜態文件和過期時間