最近一直在玩nginx,發現他配合FCGI相對apache確實是一個比較適合建站的web代理,在我的512M記憶體的VPS上初始配置下跑LAMP開啟服務的瞬間記憶體直接被佔滿,但LNMP則佔用100多M,而且訪問速度還較apache的快上一些,APACHE真心該好好最佳化最佳化了。
這裡順便把awstats的配置從apache移植到nginx,供大家參考。。
我的部落格新站已經建好,更多新的內容即將在新站更新。。
歡迎訪問http://www.showerlee.com
系統內容:centOS6.3
NGINX: nginx-1.41
AWSTATS: awstats-7.1.1
一.下載解壓awstats到系統目錄
# wget http://sourceforge.net/projects/awstats/files/AWStats/7.1.1/awstats-7.1.1.tar.gz/download
# tar -zxvf awstats-7.1.1.tar.gz -C /usr/local/
# cd /usr/local/
# mv awstats-7.1.1 awstats
# cd awstats/tools/
二.組建組態檔案:
# perl awstats_configure.pl
------------------------------
----- AWStats awstats_configure 1.0 (build 1.9) (c) Laurent Destailleur -----
This tool will help you to configure AWStats to analyze statistics for
one web server. You can try to use it to let it do all that is possible
in AWStats setup, however following the step by step manual setup
documentation (docs/index.html) is often a better idea. Above all if:
- You are not an administrator user,
- You want to analyze downloaded log files without web server,
- You want to analyze mail or ftp log files instead of web log files,
- You need to analyze load balanced servers log files,
- You want to 'understand' all possible ways to use AWStats...
Read the AWStats documentation (docs/index.html).
-----> Running OS detected: Linux, BSD or Unix
-----> Check for web server install
Enter full 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
Your web server config file(s) could not be found.
You will need to setup your web server manually to declare AWStats
script as a CGI, if you want to build 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 a new AWStats config/profile
file (required if first install) [y/N] ? y
-----> Define config file name to create
What is the name of your web site or profile analysis ?
Example: www.mysite.com
Example: demo
Your web site, virtual server or profile name:
> www.abc.com.cn
-----> Define config file path
In which directory do you plan to store your config file(s) ?
Default: /etc/awstats
Directory path to store config file(s) (Enter for default): 斷行符號
>
-----> Create config file '/etc/awstats/awstats.www.showerlee.com.conf'
Config file /etc/awstats/awstats.www.showerlee.com.conf created.
-----> Add update process inside a scheduler
Sorry, configure.pl does not support automatic add to cron yet.
You can do it manually by adding the following command to your cron:
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.showerlee.com
Or if you have several config files and prefer having only one command:
/usr/local/awstats/tools/awstats_updateall.pl now
Press ENTER to continue... 斷行符號
A SIMPLE config file has been created: /etc/awstats/awstats.www.showerlee.com.conf
You should have a look inside to check and change manually main parameters.
You can then manually update your statistics for 'www.showerlee.com' with command:
> perl awstats.pl -update -config=www.showerlee.com
You can also build static report pages for 'www.showerlee.com' with command:
> perl awstats.pl -output=pagetype -config=www.showerlee.com
Press ENTER to finish... 斷行符號
----------------------
一路預設斷行符號即可完成awstats設定檔嚮導,該設定檔儲存在/etc/awstats/下
三.配置awstats
# vi /etc/awstats/awstats.www.abc.com.cn.conf
修改如下幾處配置
-------------
LogFile="/usr/local/nginx/logs/access_log"
#nginx若建立分段日誌見修改如下
LogFile="/usr/local/nginx/logs/www.abc.com.cn-%YYYY%MM%DD.access_log"
LogType=W
LogFormat=1
#此處log目錄必須具有寫入許可權
DirData="/usr/local/nging/html/www.abc.com.cn/awstats/log"
AllowToUpdateStatsFromBrowser=1
#顯示簡體中文頁面
Lang="cn"
------------
四.nginx配置:
1.nginx虛擬機器主機日誌配置:
# vi /usr/local/nginx/conf/nginx.conf
將http層級的如下行注釋去掉
---------------------------
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
---------------------------
在需要監控的www.abc.com.cn虛擬機器主機的server層級添加
--------------------------
access_log /usr/local/nginx/logs/www.abc.com.cn.access_log;
--------------------------
2.利用指令碼分段切割nginx日誌格式為awstats可識別格式
注:apache在這裡是利用內建rotatelogs命令實現日誌每日分頁切割
# vi /etc/rc.d/nginx_cut.sh
---------------------------
#!/bin/bash
# nginx日誌位置
logs_path="/usr/local/nginx/logs/"
# nginx虛擬目錄日誌名
domain_name=www.abc.com.cn
log_name="${logs_path}${domain_name}.access_log"
# 判斷nginx日誌是否產生,如果產生則將日誌內容增量到awstats可識別格式日誌,並將nginx日誌內# 容清空,否則重啟nginx服務,重建記錄檔
if [ -e "$log_name" ];
then
cat $log_name >> $logs_path$domain_name-$(date +"%Y%m%d").access_log
cat /dev/null > $log_name
else
/etc/init.d/nginx restart
fi
---------------------------
注:這裡的date +"%Y%m%d"需與/etc/awstats/awstats.www.abc.com.cn.conf下
LogFile="/usr/local/nginx/logs/www.abc.com.cn-%YYYY%MM%DD.access_log"格式對應,否則無法寫入日誌資料
3.awstats原始碼配置:
1).建立www.abc.com.cn虛擬目錄下awstats二級連結
# mkdir -p /usr/local/nginx/html/www.abc.com.cn/awstats
2).設定log目錄可寫入權限,用於存放轉化後的日誌資料
# cd /usr/local/nginx/html/www.abc.com.cn/awstats
# mkdir log/ && chmod -R 777 log
3).複製awstats頁面需要的原始碼(icon、css等)
# cd /usr/local/awstats/wwwroot/
# cp -R icon /usr/local/nginx/html/www.abc.com.cn/awstats
# cp -R css /usr/local/nginx/html/www.abc.com.cn/awstats
# cp -R classes /usr/local/nginx/html/www.abc.com.cn/awstats
# cp -R js /usr/local/nginx/html/www.abc.com.cn/awstats
# cd /usr/local/nginx/html/www.abc.com.cn/awstats/
# chmod -R 755 icon css classes js
4.編寫awstats靜態頁面指令碼:
# vi /etc/rc.d/awstats.sh
版本一隻能產生一級連結日誌分析)
-------------
#/bin/bash
PERL=/usr/bin/perl
awstats=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl
$PERL $awstats -update -config=www.abc.com.cn
$PERL $awstats -config=www.abc.com.cn -output -staticlinks > /usr/local/nginx/html/www.abc.com.cn/awstats/index.html
-------------
版本二產生多級連結日誌分析,推薦)
---------------
#/bin/bash
########################################
PERL=/usr/bin/perl
awstats=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl
hostdir=/usr/local/nginx/html/www.abc.com.cn/awstats
buildstaticpages=/usr/local/awstats/tools/awstats_buildstaticpages.pl
########################################
$buildstaticpages -update -config=www.abc.com.cn -lang=cn -dir=$hostdir -awstatsprog=$awstats
---------------
添加一個首頁的軟鏈:
# cd /usr/local/nginx/html/www.abc.com.cn/awstats/
# ln -s awstats.www.abc.com.cn.html index.html
5.加入計劃任務:
實現每分鐘切割日誌並產生靜態awstats頁面
# crontab -e
-------------
* * * * * /bin/sh /etc/rc.d/nginx_cut.sh
* * * * * /bin/sh /etc/rc.d/awstats.sh
-------------
重啟計劃任務
# service crond restart
6.添加nginx alias功能,防止awstats頁面暴圖:
# vi /usr/local/nginx/conf/nginx.conf
在www.abc.com.cn虛擬機器主機的server層級添加
-----------------
autoindex on;
location /awstatscss {
alias /usr/local/nginx/html/www.abc.com.cn/awstats/css/;
}
location /awstatsicons {
alias /usr/local/nginx/html/www.abc.com.cn/awstats/icon/;
}
location /awstatsclasses {
alias /usr/local/nginx/html/www.abc.com.cn/awstats/classes/;
}
-----------------
重啟nginx
# service nginx restart
7.添加nginx目錄訪問認證功能
1).下載htpasswd.sh指令檔並執行
# wget -c http://soft.vpser.net/lnmp/ext/htpasswd.sh
# sh htpasswd.sh
按提示輸入使用者名稱、密碼、及認證檔案名稱。指令碼會自動產生認證檔案
---------------------------------
=====================================
# A tool like htpasswd for Nginx #
#-----------------------------------#
# Author:Licess http://www.lnmp.org #
=====================================
Please input UserName:awstats
===========================
UserName was: awstats
===========================
Please input the Password:123456
===========================
Password was: 123456
===========================
Please input Auth filename:awstat_auth
===========================
Auth File: /usr/local/nginx/conf/awstat_auth
===========================
Press any key to Creat...or Press Ctrl+c to cancel 斷行符號
Create Auth file......
Create Auth file successful,auth file path:/usr/local/nginx/conf/awstat_auth.conf.
-------------------------------------
2).為Nginx添加認證配置:
# vi /usr/local/nginx/conf/nginx.conf
在www.abc.com.cn虛擬機器主機的server層級添加:
-------------------------
location ^~ /awstats/ {
auth_basic "Authorized users only";
auth_basic_user_file /usr/local/nginx/conf/awstat_auth.conf;
}
-------------------------
注:
"Authorized users only"為登陸介面提示資訊
"/usr/local/nginx/conf/awstat_auth.conf" 為htpasswd.sh指令碼返回的認證檔案的路徑,內含明文的登陸名和加密的密碼
nginx.conf下www.abc.com.cn虛擬目錄server層級的完整配置如下:
------------------------------
server {
listen 80;
server_name www.abc.com.cn;
index index.html index.htm index.php;
root /usr/local/nginx/html/www.abc.com.cn;
# fcgi+php支援
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/www.abc.com.cn$fastcgi_script_name;
include fastcgi_params;
}
# PHP偽靜態
if (!-e $request_filename) {
rewrite ^/[_0-9a-zA-Z-]+(/wp-.*) $1 last;
rewrite ^/[_0-9a-zA-Z-]+.*(/wp-admin/.*\.php)$ $1 last;
rewrite ^/[_0-9a-zA-Z-]+(/.*\.php)$ $1 last;
}
# 虛擬目錄訪問日誌
access_log /usr/local/nginx/logs/www.abc.com.cn.access_log;
# awstats防暴圖
autoindex on;
location /awstatscss {
alias /usr/local/nginx/html/www.abc.com.cn/awstats/css/;
}
location /awstatsicons {
alias /usr/local/nginx/html/www.abc.com.cn/awstats/icon/;
}
location /awstatsclasses {
alias /usr/local/nginx/html/www.abc.com.cn/awstats/classes/;
}
# awstats頁面認證
location ^~ /awstats/ {
auth_basic "Authorized users only";
auth_basic_user_file /usr/local/nginx/conf/awstat_auth.conf;
}
}
------------------------------
3).重啟nginx
# service nginx restart
五.Web查看nginx+awstats流量監控日誌.
1.瀏覽器地址欄輸入 http://www.abc.com.cn/awstats
註:實驗環境訪問頁面需修改用戶端Hosts檔案
提示輸入使用者名稱和密碼
650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131228/1129391207-0.jpg" style="float:none;" title="1.jpg" />
日誌分析首頁:
650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131228/1129394144-1.jpg" style="float:none;" title="2.jpg" />
大功告成。。。。。
本文出自 “一路向北” 部落格,請務必保留此出處http://showerlee.blog.51cto.com/2047005/1275718