1. 程式人生 > >nginx日誌不記錄靜態文件訪問和緩存

nginx日誌不記錄靜態文件訪問和緩存

主機名 ranges 請求 任務 add 再次 瀏覽器中 localhost 小時

nginx訪問日誌
nginx和apache的訪問日誌一樣可以記錄的指定信息,如記錄服務器時間,訪問的客戶端ip、訪問的url和訪問狀態碼等信息,這些信息會規律的記錄到訪問日誌中
主配置文件中定義的日誌格式,記錄的格式參數解釋如下

$remote_addr ? ? ? ? ? ? ? ? ?  客戶端訪問IP(公網IP)
$http_x_forwarded_for ? ? ? ? ? 記錄代理服務器的IP
$time_local ? ? ? ? ? ? ? ? ? ? 日誌中服務器本地時間
$host ? ? ? ? ? ? ? ? ? ? ? ? ? 記錄客戶端訪問的主機名(域名)
$request_url ? ? ? ? ? ? ? ? ?  記錄訪問的URL地址(域名後的路徑信息)
$status ? ? ? ? ? ? ? ? ? ? ? ? 狀態碼,記錄客戶端訪問時返回的訪問狀態碼,如200、302、404這些狀態
$http_referer ? ? ? ? ? ? ? ? ? HTTP Referer是header的一部分,當瀏覽器向web服務器發送請求的時候,一般會帶上Referer,告訴服務器我是從哪個頁面鏈接過來的,服務器基此可以獲得一些信息用於處理。
$http_user_agent ? ? ? ? ? ? ?  記錄客戶端訪問瀏覽器的標識,可用於限制一些瀏覽器禁止訪問,如網絡爬蟲的訪問類型

在nginx主配置文件nginx.conf配置記錄的日誌類型,combined_realip是定義記錄的引用名字,這個名稱會在虛擬主機配置文件中用到。日誌記錄的格式是以單引號擴起來的,可以換行輸入,這裏nginx定義了虛擬主機配置文件,還需要在虛擬主機配置文件中定義日誌的保存路徑

 ?  log_format zidingyi ‘$remote_addr $http_x_forwarded_for [$time_local]‘
 ? ?‘ $host "$request_uri" $status‘
 ? ?‘ "$http_referer" "$http_user_agent"‘;

配置完主配置文件後,還需要定義下日誌記錄信息的保存路徑,這裏的日誌保存路徑是分虛擬主機的,不同的虛擬主機可以把日誌存放到不同的路徑下,這樣也方便區分,日誌定義在server模塊的下面

 ? if ($host != ‘test.com‘) {
 ? ? ? ?  rewrite ^/(.*)$ http://aaa.com/$1 permanent;
 ? }
 ? ?  access_log /data/wwwroot/log/aaa.com.log zidingyi;
}
重新reload下nginx的配置,使用curl測試訪問並查看記錄的日誌格式
[root@localhost conf]# /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 conf]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost conf]# curl -x127.0.0.1:80 ccc.com -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.15.2
Date: Tue, 14 Aug 2018 08:32:14 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://aaa.com/
[root@localhost conf]# curl -x127.0.0.1:80 ddd.com -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.15.2
Date: Tue, 14 Aug 2018 08:32:22 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://aaa.com/
[root@localhost conf]# cat /data/wwwroot/log/aaa.com.log 
127.0.0.1 - [14/Aug/2018:16:32:14 +0800] ccc.com "/" 301 "-" "curl/7.29.0"
127.0.0.1 - [14/Aug/2018:16:32:22 +0800] ddd.com "/" 301 "-" "curl/7.29.0"

nginx日誌切割
nginx日誌切割的話沒有apache那種方便的工具,需要借助切割工具或者要麽我們編寫一個定時執行的清理腳本,腳本功能把日誌文件每天定時移動重命名並重新讓nginx生成新的日誌文件(使用/bin/kill -HUP操作) ,把日誌以日期的形式重命名存放,再使用find配合-exec查找並刪除多少天之前的日誌文件,並把執行腳本執行命令寫入計劃任務,每天淩晨執行一次,其切割腳本參考內容如下:

[root@localhost nginx]# vim /usr/local/sbin/nginx_logrotate.sh 
#!/bin/bash
date=`date -d "-1 day " +%Y%m%d`
logdir="/data/wwwroot/log/"
nginx_pid="/usr/local/nginx/logs/nginx.pid"
cd $logdir
for log in `ls *.log`
do
 ? ?mv $log $log-$date
done
/bin/kill -HUP `cat $nginx_pid`
find $logdir -name *.log-.* -type f -mtime +30 -exec rm -rf {} \;

日誌切割後需要重新讓nginx生成新的日誌,這樣nginx才能繼續記錄的日誌
腳本中涉及for循環操作,有關shell循環語法請查閱其他相關資料

nginx不記錄文件訪問日誌和靜態文件過期時間
nginx和apache一樣支持訪問某些文件類型時URL中不記錄其文件記錄的日誌信息,同樣也支持靜態文件在瀏覽器中緩存的過期時間,過期時間也是通過location來定義配置的,其配置文件內容如下,統配文件名稱和以類型結尾來匹配:

[root@localhost nginx]# vim conf/vhost/aaa.conf
 ? if ($host != ‘test.com‘) {
 ? ? ? ?  rewrite ^/(.*)$ http://aaa.com/$1 permanent;
 ? }
 ? ?  location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
 ? ? ?  {
 ? ? ? ? ? ? ? expires 7d; ? ? ? ? ? ? ?---------圖片文件設置7天的過期時間
 ? ? ? ? ? ? ? access_log off;
 ? ? ?  }
 ? ?  location ~ .*\.(js|css)$
 ? ? ?  {
 ? ? ? ? ? ? ? expires 8h; ? ? ? ? ? ? ?-------設置8小時的過期時間
 ? ? ? ? ? ? ? access_log off;
 ? ? ?  }
[root@localhost nginx]# /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 nginx]# /usr/local/nginx/sbin/nginx -s reload

nginx配置文件確認配置沒有錯誤重啟後,創建以css結尾的文件訪問測試下日誌是否記錄

[root@localhost aaa]# curl -x127.0.0.1:80 -I aaa.com/index.php
HTTP/1.1 200 OK
Server: nginx/1.15.2
Date: Tue, 14 Aug 2018 10:29:00 GMT
Content-Type: application/octet-stream
Content-Length: 26
Last-Modified: Fri, 10 Aug 2018 17:28:28 GMT
Connection: keep-alive
ETag: "5b6dcb3c-1a"
Accept-Ranges: bytes ? ? ? ? ? ? ? ? ? ? ? ? ? ? --------訪問php頁面不進行靜態文件緩存
[root@localhost aaa]# curl -x127.0.0.1:80 -I aaa.com/2.css
HTTP/1.1 200 OK
Server: nginx/1.15.2
Date: Tue, 14 Aug 2018 10:29:12 GMT
Content-Type: text/css
Content-Length: 11
Last-Modified: Tue, 14 Aug 2018 10:04:01 GMT
Connection: keep-alive
ETag: "5b72a911-b"
Expires: Tue, 14 Aug 2018 18:29:12 GMT
Cache-Control: max-age=28800
Accept-Ranges: bytes ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?----------這裏訪問css文件時cache的時間為28800秒/8小時
[root@localhost aaa]# curl -x127.0.0.1:80 -I aaa.com/1.gif
HTTP/1.1 200 OK
Server: nginx/1.15.2
Date: Tue, 14 Aug 2018 10:29:24 GMT
Content-Type: image/gif
Content-Length: 6
Last-Modified: Tue, 14 Aug 2018 10:11:07 GMT
Connection: keep-alive
ETag: "5b72aabb-6"
Expires: Tue, 21 Aug 2018 10:29:24 GMT
Cache-Control: max-age=604800
Accept-Ranges: bytes ? ? ? ? ? ? ? ? ? ? ? ? ? ? -----------圖片文件過期時間問604800秒,即7天
[root@localhost aaa]# cat /data/wwwroot/log/aaa.com.log
127.0.0.1 - [14/Aug/2018:18:29:00 +0800] aaa.com "/index.php" 200 "-" "curl/7.29.0"

通過測試看出指定的文件被緩存到瀏覽器中,過期時間是服務器上指定的,如果再次訪問該文件時則瀏覽器標識則會顯示為304,而日誌記錄中不會對該類型的文件訪問做記錄

nginx日誌不記錄靜態文件訪問和緩存