Nginx配置文件實(shí)現(xiàn)AWStats靜態(tài)頁(yè)面
Nginx配置文件在有些時(shí)候需要我們不斷的更新,雖然AWStats現(xiàn)在已經(jīng)支持輸出靜態(tài)網(wǎng)頁(yè)形式的分析結(jié)果,但是頁(yè)面布局沒有用Perl生成的動(dòng)態(tài)網(wǎng)頁(yè)方便,而且用自帶的用Perl編寫的轉(zhuǎn)換工具稍微有點(diǎn)慢,在流量不大的情況下,還是在Perl+Fast CGI上運(yùn)行更舒服一些。
首先要參考之前的文章,建立好PERL+FCGI的運(yùn)行環(huán)境,這步準(zhǔn)備工作就夠費(fèi)勁的。
然后開始切入正題:
1. 下載***版的AWStats,基本就是Perl包,所以沒必要用apt-get,解壓縮到/usr/local/awstats下
wget http://prdownloads.sourceforge.net/awstats/awstats-6.95.tar.gz
tar -xzf awstats-6.95.tar.gz
mv awstats-6.95 /usr/local/awstats
2. 創(chuàng)建一個(gè)存放awstats分析數(shù)據(jù)的目錄
mkdir /var/lib/awstats
chown www-data /var/lib/awstats //這是為了讓awstats頁(yè)面上能直接刷新***數(shù)據(jù)
3. 自動(dòng)配置awstats
cd /usr/local/awstats/tools
perl awstats_configure.pl
除了***步因?yàn)槭荖gin服務(wù)器的關(guān)系,所以要選none,其他基本按照提示選默認(rèn)值
4. 手工編輯Nginx配置文件
1) 修改LogFile路徑
LogFile = “/var/log/Nginx/access.log”
如果是壓縮格式的日志,可以用LogFile = “zcat /var/log/Nginx/%YYYY-24%MM-24%DD-24.gz|"。這里用zcat是因?yàn)槠涫褂霉艿垒敵?,?duì)系統(tǒng)資源消耗比較小,千萬不要忘了***的管道操作符!
假設(shè)原來/etc/Nginx/Nginx.conf中關(guān)于log部分是如此定義的:(要小心各個(gè)變量之間必須添加的空格,不能少,否則awstats就不認(rèn)了)
log_format main ‘$remote_addr $remote_user [$time_local] “$request” $status ‘
‘$host $body_bytes_sent $gzip_ratio “$http_referer” ‘
‘”$http_user_agent” “$http_x_forwarded_for”‘;
很容易知道,對(duì)應(yīng)awstats配置文件中,LogFormat應(yīng)該設(shè)置為
LogFormat= “%host %logname %time1 %methodurl %code %host_r %bytesd %gzipratio %refererquot %uaquot %otherquot”
***一個(gè)選%otherquot是應(yīng)為$http_x_forwarded_for在AWstats 6.95中還不認(rèn)識(shí)
3) 將AllowToUpdateStatsFromBrowser=1,便于瀏覽器中即時(shí)刷新數(shù)據(jù)
5. 更新Awstats的分析記錄,測(cè)試一下剛才的配置
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=yousite.com
如果一切正常,應(yīng)該看到類似以下的結(jié)果:
Create/Update database for config “/etc/awstats/awstats.yoursite.com.conf” by AWStats version 6.95 (build 1.943)
From data in log file “/var/log/Nginx/access.log”…
Phase 1 : First bypass old records, searching new record…
Searching new records from beginning of log file…
Phase 2 : Now process new records (Flush history on disk after 20000 hosts)…
Jumped lines in file: 0
Parsed lines in file: 1234
Found 0 dropped records,
Found 0 corrupted records,
Found 0 old records,
Found 1234 new qualified records.
6. 修改logrotate.d下的Nginx配置文件,在每天切割日志前,更新awstats狀態(tài)
- /var/log/ nginx /*.log {
- daily
- missingok
- rotate 7
- compress
- delaycompress
- notifempty
- create 640 www-data www-data
- dateext
- sharedscripts
- prerotate
- /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=yoursite.com
- sleep 59
- endscript
- postrotate
- if [ -f /var/run/nginx.pid ]; then
- kill -USR1 `cat /var/run/nginx.pid`
- fi
- endscript
- }
7. 接下來是最關(guān)鍵的NGINX配置文件
- #AWStatus Perl CGI server
- server {
- listen 80;
- server_name awstats.yoursite.com;
- access_log /var/log/nginx/awstats.log main;
- error_log /var/log/nginx/awstats_error.log; #這可以為fail2ban留下記錄
- root /usr/local/awstats/wwwroot;
- auth_basic “Restricted”;
- auth_basic_user_file /etc/nginx/awstatus.pass;
- location = / {
- rewrite ^ /awstats.pl?config=freshventure.info;
- }
- location ~ .*(\.cgi|\.pl?)$ {
- gzip off; #gzip makes scripts feel slower since they have to complete before getting gzipped
- root /usr/local/awstats/wwwroot/cgi-bin;
- fastcgi_pass 127.0.0.1:8000;
- fastcgi_index awstats.pl;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- include fastcgi_params;
- }
- }
好了,測(cè)試一下http://awstats.yoursite.com,和前文一樣,輸入密碼后,這次看到的應(yīng)該就awstats的界面了
8. 如果需要配置靜態(tài)頁(yè)面,則可以在logrotate中替換掉上面的awstats.pl, 換成
/usr/local/awstats/tools/awstats_buildstaticpages.pl -update -config=yoursite.com -lang=cn -dir=/usr/local/awstats/wwwroot -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl。
如果需要更頻繁的更新訪問情況,則可以把這句命令加入到crontab中,例如crontab -e -uwww-data。
然后nginx配置文件相應(yīng)調(diào)整為顯示靜態(tài)網(wǎng)頁(yè)就可以了。
【編輯推薦】