nginx日誌格式設定,去掉以下行#號php
#vim /usr/local/nginx/conf/nginx.conf 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 log/access.log main;
參數說明:html
$remote_addr 與$http_x_forwarded_for 用以記錄客戶端的ip地址; $remote_user :用來記錄客戶端用戶名稱; $time_local :用來記錄訪問時間與時區; $request :用來記錄請求的url與http協議; $status :用來記錄請求狀態;成功是200, $body_bytes_s ent :記錄發送給客戶端文件主體內容大小; $http_referer :用來記錄從那個頁面連接訪問過來的; $http_user_agent :記錄客戶毒啊瀏覽器的相關信息;
nginx日誌切割腳本nginx
#vim /usr/local/nginx/nginx_log.sh #!/bin/bash logs_path="/usr/local/nginx/logs/" #設置日誌文件存放目錄 pid_path="/usr/local/nginx/logs/nginx.pid" #設置pid文件 mv${logs_path}access.log ${logs_path}access_$(date -d "yesterday" +"%Y%m%d").log #重命名日誌文件,將access.log切割爲昨天的日誌文件,由於要在天天零點切割,全部日誌文件格式是前一天的日期,如access_20130728.log kill-USR1 `cat${pid_path}` #向nginx主進程發信號從新打開日誌
安裝和配置awstatsgit
wget http://awstats.sourceforge.net/files/awstats-7.0.tar.gz tar -xvf awstats-7.0.tar.gz mv awstats-7.0 /usr/local/awstats chown -R root:root /usr/local/awstats chmod -R =rwX /usr/local/awstats chmod +x /usr/local/awstats/tools/\*.pl chmod +x /usr/local/awstats/wwwroot/cgi-bin/\*.pl 執行 tools 目錄中的 awstats_configure.pl 配置嚮導,建立一個新的統計 cd /usr/local/awstats/tools ./awstats_configure.pl 將會有以下一些提示: -----> Running OS detected: Linux, BSD orUnix ----->Check for web server install Enterfull config file path of your Web server. Example:/etc/httpd/httpd.conf Example:/usr/local/apache2/conf/httpd.conf Example:c:\Program files\apache group\apache\conf\httpd.conf Config file path ('none' to skip web server setup): >none #這裏添none並回車,由於咱們沒有使用apache 回車以後下一個選項: Yourweb server config file(s) could not be found. Youwill need to setup your web server manually to declare AWStats script as a CGI, if you want tobuild reports dynamically. See AWStats setup documentation (file docs/index.html) ----->Update model config file '/usr/local/awstats/wwwroot/cgi-bin/awstats.model.conf' File awstats.model.conf updated. ----->Need to create a new config file ? Do you want me to build anew AWStats config/profilefile (required if first install) [y/N] ? y #這裏選Y,建立一個新的配置文件 -----> Define config file name to create What is the name of your web site or profile analysis ? Example: www.mysite.com Example: demo Yourweb site, virtual server or profile name: >nginx #這裏輸入你要分析的域名,或是隨便一個你易記的配置名並回車 ----->Define config file path In which directory do you plan to store your config file(s) ? Default: /etc/awstats Directorypath to store config file(s) (Enter fordefault): > #直接回車,定義你的配置文件存放的路徑,使用默認路徑/etc/awstats ----->Create config file '/etc/awstats/awstats.nginx.conf' Configfile /etc/awstats/awstats.nginx.conf created. -----> Add updateprocess inside a scheduler Sorry, configure.pl does not support automatic addto cron yet. You can do it manually by adding the following command to yourcron: /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=nginx Or if you have several config files and prefer having only onecommand: /usr/local/awstats/tools/awstats_updateall.pl now Press ENTER to continue...#按回車繼續 A SIMPLE config file has been created: /etc/awstats/awstats.nginx.conf You should have a lookinside to check and change manually main parameters. Youcan then manually update your statistics for'yuyuanchun.com' withcommand: > perl awstats.pl -update -config=nginx Youcan also build static report pages for'nginx' with command:> perl awstats.pl -output=pagetype -config=nginx PressENTER to finish... #回車完成配置文件的建立
默認會生成一個名爲awstats.nginx.conf配置文件在/etc/awstats/目錄下,修改該配置文件的日誌位置web
vim /etc/awstats/awstats.nginx.conf LogFile="/usr/local/nginx/logs/access\_%YYYY-0%MM-0%DD-24.log" 這裏是對應上面Nginx日誌切割所生成的目錄存放位置,注意awstats的年月日格式,-24表示昨天的日誌,-0表示當前的 分析的執行順序是: Nginx 產生日誌 –> 日誌切割 –> Nginx 繼續產生日誌 –> 另存切割日誌 –> 交由Awstats統計 –> 生成結果
日誌說明apache
# "LogFile" contains the web, ftp or mail server logfile to analyze. #You can also use tags inthis filename if you need a dynamic file name #depending on date or time (Replacement is made by AWStats at the beginning #ofits 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=24if you need (1-7, 1=monday) # %Dw-n is replaced with the day number in week (0-6, 0=sunday) #use n=24if you need (0-6, 0=monday) #Use 0for n if you need current year, month, day, hour... n表示時間間隔,我這裏就是分析距離當前今天24小時前(昨日)的日誌,改爲48就是前兩天的日誌
建立一個awstats用於記錄數據的目錄vim
mkdir -p /var/lib/awstats 而後運行awstats的wwwroot目錄中的awatsts.pl來測試一下 /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=nginx
出現報錯:瀏覽器
Fromdata in log file "/usr/local/nginx/logs/access_20131111.log"... Error:Couldn't open server log file "/usr/local/nginx/logs/access_20131111.log" : 沒有那個文件或目錄 解決辦法: touch /usr/local/nginx/logs/access_20131111.log 再次執行 /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=nginx 若是看到相似下面的提示就說明配置文件都正確了 Create/Updatedatabase for config "/etc/awstats/awstats.nginx.conf" by AWStats version 7.0 (build1.964) From data in log file "/home/wwwlogs/access.log_20130727"... Phase 1 : First bypass old records, searching newrecord... Directaccess after last parsed record (after line 43260) Jumped lines in file: 43260 Found 43260already parsed records. Parsedlines in file: 0 Found0 dropped records, Found0 comments, Found0 blank records, Found0 corrupted records, Found0 old records, Found0new qualified records
Nginx 對 Perl 支持並很差,因此這裏利用 awstats 的工具將統計的結果生成靜態文件bash
首先在 webroot 目錄下建立一個文件夾。例:/home/www/awstats mkdir -p /home/www/awstats
生成靜態文件:ide
/usr/local/awstats/tools/awstats_buildstaticpages.pl -update -config=nginx -lang=cn-dir=/home/www/awstats/ -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl 運行腳本若是出現生成一堆網頁相似爲正確 Launchupdate process : "/usr/local/awstats/wwwroot/cgi-bin/awstats.pl" -config=nginx -update -configdir= ...... Buildkeywords page: "/usr/local/awstats/wwwroot/cgi-bin/awstats.pl" -config=nginx -staticlinks -lang=cn-output=keywords Build errors404 page: "/usr/local/awstats/wwwroot/cgi-bin/awstats.pl" -config=nginx-staticlinks -lang=cn -output=errors404 20 files built. Main HTML page is'awstats.nginx.html'. /usr/local/awstats/tools/awstats_buildstaticpages.pl Awstats靜態頁面生成工具 -update -config=nginx 更新配置項 -lang=cn 語言爲中文 -dir=/home/www/awstats 統計結果輸出目錄 -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl Awstats 日誌更新程序路徑
修改nginx配置文件,添加日誌分析結果站點
#vim /usr/local/nginx/conf/nginx.conf 在最末尾添加以下行 server { listen 8080; server_name localhost; index index.html index.htmindex.php default.html default.htm default.php; root /home/www/awstats; location ~ ^/awstats/ { #html靜態頁面目錄 root /home/www/awstats; index index.html; access_log off; } location ^~ /icon/ { #圖標目錄 root /usr/local/awstats/wwwroot; index index.html; access_log off; } autoindex on; #開啓目錄瀏覽功能 access_log off; }
從新加載nginx
ln -s /usr/local/nginx/sbin/nginx /usr/bin/ nginx –t #檢查nginx配置文件是否有錯誤 nginx –s reload #加載
添加定時任務,每晚切割,分析nginx日誌
# vim /usr/local/nginx/awstats.sh #!/bin/bash /bin/bash /usr/local/nginx/nginx_log.sh /usr/local/awstats/wwwroot/cgi-bin/awstats.pl-update -config=nginx /usr/local/awstats/tools/awstats_buildstaticpages.pl -update -config=nginx -lang=cn-dir=/home/www/awstats/ -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl #chmod +x /usr/local/nginx/awstats.sh #crontab -e 01 00 * * * /usr/local/nginx/awstats.sh
這樣訪問http://192.168.233.210:8080 就能夠看到統計結果靜態頁面了。