nginx日誌分割處理以及分析

在不少時候,咱們會很是關注網站的訪問量,好比網站的日PV是多少、網站某個功能上線以後點擊量是多少,像這些東西都是須要從web容器中的訪問日誌統計出來的,下面咱們看一下如何在nginx中統計網站的訪問信息


一、設置Nginx訪問日誌記錄格式
在默認狀況下,nginx只是記錄相關get信息,像post頁面是不記錄的,因此下面須要修改nginx.conf,讓其訪問日誌記錄post等請求信息,在nginx.conf中server段中加入以下信息

        log_format  access  '$remote_addr - $remote_user [$time_local] "$request"' '$status $body_bytes_sent "$http_referer"' '"$http_user_agent" $http_x_forwarded_for';
        access_log /usr/local/nginx/logs/access.log access;


二、設置日誌按期截取
設置日誌按期截取一是爲了方便查閱,二是爲了I/O擁塞(截止到目前筆者維護過的服務器中單臺服務器日訪問日誌大小就達到1.6G,若是不按期截取,因爲文件內容較大,後期對文件進程查詢、移動時將會嚴重影響系統性能)。nginx日誌格式不像apache、resin那麼人性化,nginx訪問日誌沒法在nginx的配置文件中設置成按日期格式存儲,目前常見的設置方法主要靠第三方工具或者腳原本實現,下面咱們就經過一個最簡單的腳本進行實現

#vi /etc/nginx_access_log.sh
#!/bin/bash
mv /usr/local/nginx/logs/access.log /opt/nginx_access_`date +%Y%m%d`.log
killall -s USR1 nginx

腳本說明:這個腳本主要實現兩個功能,一是將nginx訪問日誌按照日期移動到目的地,而是移動完畢後讓nginx從新生成日誌文件

#chmod +x /etc/nginx_access_log.sh

使用cron服務按期執行該腳本,下面設置成的是每晚23點59執行,這樣nginx訪問日誌正好記錄的是全天的訪問記錄
#crontab -e
59 23 * * * /etc/nginx_access_log.sh


三、日誌查詢
下面作一個最簡單的統計,統計http://blog.luwenju.com頁面的日點擊量是多少
#grep -c 'http://blog.luwenju.com/' /opt/nginx_access_20110815.log
396

總結:像本篇文章所介紹的統計方法只適合訪問量較小、應用相對簡單的網站,像複雜應用、訪問量較大的網站還須要藉助第三方工具來統計,目前應用最普遍的是awstats,《Nginx日誌分析(下)》將介紹如何使用awstats來分析nginx日誌 php

http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=3582157 html

 

 

第一步,日誌的處理linux

最好天天分割一下日誌,一開始沒注意這個工做,結果不久日誌文件就上G了,很痛苦。分割日誌很簡單,首先把日誌文件複製到別的地方,而後再通知nginx從新生成日誌就能夠了。shell腳本以下: nginx

 

# !/bin/bash

logs_path
= " /nginx/logs/ "
mv ${logs_path}access . log  ${logs_path}access_$(date  - " yesterday "   + " %Y%m%d " ) . log
kill   - USR1 `cat  / nginx / logs / nginx . pid`

 

代碼中的/nginx/logs指的是nginx的log日誌文件所在目錄,生成了以昨天日期命名的日誌文件。 git

爲了達到天天自動分割的目的,在crontab中加入如下部分: web

 

1   0   *   *   *  sh  / home / zyf / sh / cut_nginx_log . sh

這樣就天天的0點1分把nginx日誌重命名爲日期格式,並從新生成今天的新日誌文件。 shell

 

第二步,Awstats的配置。

日誌文件分割好了,接下來就是分析了,也就是Awstats的使用了。
Awstats的配置文件默認會存儲在/etc/awstats/目錄下,包括你安裝時設置的域名如:awstats.www.xxxxke.com.conf。在這個配置文件中修改這個地方:
LogFile = " /nginx/logs/access_%YYYY-0%MM-0%DD-24.log "

這個意思是要去讀取nginx昨天的日誌文件,關於後邊%YYYY-0%MM-0%DD-24的設置,規則以下: apache

 

複製代碼
#  You can also use tags in this filename if you need a dynamic file name
# depending on date or time (Replacement is made by AWStats at the beginning
# of its execution). This is available tags :
#   %YYYY-n  is replaced with 4 digits year we were n hours ago
#   %YY-n    is replaced with 2 digits year we were n hours ago
#   %MM-n    is replaced with 2 digits month we were n hours ago
#   %MO-n    is replaced with 3 letters month we were n hours ago
#   %DD-n    is replaced with day we were n hours ago
#   %HH-n    is replaced with hour we were n hours ago
#   %NS-n    is replaced with number of seconds at 00:00 since 1970
#   %WM-n    is replaced with the week number in month (1-5)
#   %Wm-n    is replaced with the week number in month (0-4)
#   %WY-n    is replaced with the week number in year (01-52)
#   %Wy-n    is replaced with the week number in year (00-51)
#   %DW-n    is replaced with the day number in week (1-7, 1=sunday)
#                              use n=24 if you need (1-7, 1=monday)
#   %Dw-n    is replaced with the day number in week (0-6, 0=sunday)
#                              use n=24 if you need (0-6, 0=monday)
#   Use 0 for n if you need current year, month, day, hour

複製代碼

 

第三步,開始分析、生成結果。 bash

最後,能夠執行分析了。使用這個命令:
/ usr / local / awstats / wwwroot / cgi - bin / awstats . pl  - update  - config = www . xxxxke . com

 

這個命令會把結果生成到/var/lib/awstats 目錄下 awstatsXXXX.www.XXXX.com.txt文件。 服務器

固然啦,這樣看起來不太方便哦,呵呵,能夠再用下面的命令來生成html頁面,至關漂亮:

 


perl /usr/local/awstats/tools/awstats_buildstaticpages.pl -update \
 
-config=www.xxxxoke.com -lang=cn \ 
-dir=/html/awstats \
-awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl

 

這樣就會在/html/awstats目錄下生成很漂漂的分析結果頁,很暴力很強大。

 

第四步,自動化。

要是天天都去服務器上運行幾條命令確定是件使人煩燥的事情,因此呢,linux的世界裏有crontab這樣的好東東,很簡單,下面是個人crontab

 

1   0   *   *   *  sh  / home / zyf / sh / cut_nginx_log . sh

0   1   *   *   *   / usr / local / awstats / wwwroot / cgi - bin / awstats . pl  - update  - config = www . xxxxke . com

0   2   *   *   *  perl  / usr / local / awstats / tools / awstats_buildstaticpages . pl  - update  - config = www . xxxxke . com  - lang = cn  - dir =/ html / awstats  - awstatsprog =/ usr / local / awstats / wwwroot / cgi - bin / awstats . pl

 

-------------------------------------------------------------------------

大功告成,打完收功……

 http://www.cnblogs.com/amboyna/archive/2009/08/09/1542171.html

相關文章
相關標籤/搜索