天天看點

2018-3-14 12周3次課 Nginx通路日志、日志分割、日志不記錄靜态檔案和過期時間

12.10 Nginx通路日志

·日志格式:

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf      

搜尋log_format

2018-3-14 12周3次課 Nginx通路日志、日志分割、日志不記錄靜态檔案和過期時間

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

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

2018-3-14 12周3次課 Nginx通路日志、日志分割、日志不記錄靜态檔案和過期時間
2018-3-14 12周3次課 Nginx通路日志、日志分割、日志不記錄靜态檔案和過期時間

公網ip(出口ip)

2018-3-14 12周3次課 Nginx通路日志、日志分割、日志不記錄靜态檔案和過期時間

·除了在主配置檔案nginx.conf裡定義日志格式外,還需要在虛拟主機配置檔案中增加

access_log /tmp/test.com.log combined_realip

(日志名稱可以自己定義,combined_realip就是nginx.conf中的日志格式名)

2018-3-14 12周3次課 Nginx通路日志、日志分割、日志不記錄靜态檔案和過期時間
[root@localhost 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@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost 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

[root@localhost 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:  

[root@localhost 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

[root@localhost 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腳本:

[root@localhost vhost]# vim /usr/local/sbin/nginx_log_rotate.sh      
2018-3-14 12周3次課 Nginx通路日志、日志分割、日志不記錄靜态檔案和過期時間

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

[root@localhost 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      
2018-3-14 12周3次課 Nginx通路日志、日志分割、日志不記錄靜态檔案和過期時間

一段時間後删除早前的log檔案

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

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

[root@localhost vhost]# crontab -e

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

12.12 靜态檔案不記錄日志和過期時間

2018-3-14 12周3次課 Nginx通路日志、日志分割、日志不記錄靜态檔案和過期時間

location ~ 精準比對 任意一個以 .gif或jpg或jpeg或png或bmp或swf 為結尾的檔案

expires    配置過期時間

access_log    是否記錄通路日志

由于配置過期時間不同,是以分開寫上下兩段,js|css和上面分開

建立1.gif和2.js檔案,模拟虛拟伺服器中有這兩種格式檔案,我們來通路他們,在通路同一個虛拟主機中的index.html檔案,檢視log檔案,可以看出,log不會記錄指定格式的靜态檔案

[root@localhost vhost]# cd /data/wwwroot/test.com/
[root@localhost test.com]# vim 1.gif
[root@localhost test.com]# vim 2.js
[root@localhost test.com]# curl -x127.0.0.1:80 test.com/1.gif
ifjasdfajsdfladskf
[root@localhost test.com]# curl -x127.0.0.1:80 test.com/2.js
dlkfjad;slkfja;lk
[root@localhost test.com]# curl -x127.0.0.1:80 test.com/index.html
test.com
[root@localhost 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"
[root@localhost test.com]# curl -x127.0.0.1:80 test.com/index.html
test.com
[root@localhost 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後面跟一些其他字元,就無法比對規則,是以會記錄

[root@localhost 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>
[root@localhost 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"      

過期時間:

2018-3-14 12周3次課 Nginx通路日志、日志分割、日志不記錄靜态檔案和過期時間

如果去掉配置檔案中的 expires,那麼不會有過期時間

2018-3-14 12周3次課 Nginx通路日志、日志分割、日志不記錄靜态檔案和過期時間
[root@localhost test.com]# vim /usr/local/nginx/conf/vhost/test.com.conf
[root@localhost test.com]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost test.com]# curl -x127.0.0.1:80 test.com/2.js -I      
2018-3-14 12周3次課 Nginx通路日志、日志分割、日志不記錄靜态檔案和過期時間
2018-3-14 12周3次課 Nginx通路日志、日志分割、日志不記錄靜态檔案和過期時間

繼續閱讀