在ubuntu9.10下用awstats分析nginx日志
1 下载awstats
sudo apt-get install awstats 弄下来的版本在执行awstats_configure.pl时候找不到wwwroot目录结构,所以重新直接下载awstats了
bear@njava:~$axel http://prdownloads.sourceforge.net/awstats/awstats-6.95.tar.gz bear@njava:~$tar xzvf awstats-6.95.tar.gz
2 执行配置脚本
先把 awstats目录复制到/usr/local/awstats,脚本是按照这个目录结构去执行的,如果不是在这/usr/local/awstats里,运行时回提示的
bear@njava:/usr/local/awstats/tools$ sudo ./awstats_configure.pl [sudo] password for bear: ----- AWStats awstats_configure 1.0 (build 1.8) (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 #因为没法自动配置nginx,所以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.njava.com #配置名字 -----> 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.njava.com.conf' Config file /etc/awstats/awstats.www.njava.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.njava.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.njava.com.conf You should have a look inside to check and change manually main parameters. You can then manually update your statistics for 'www.njava.com' with command: > perl awstats.pl -update -config=www.njava.com You can also build static report pages for 'www.njava.com' with command: > perl awstats.pl -output=pagetype -config=www.njava.com Press ENTER to finish... bear@njava:/usr/local/awstats/tools$
3 修改awstats配置文件
修改awstats.conf
bear@njava:/usr/local/awstats/tools$ sudo vi /etc/awstats/awstats.conf # LogFormat = 1 # LogFormat = "%host %other %logname %time1 %methodurl %code %bytesd %refererquot %uaquot" # # Example for IIS: # LogFormat = 2 # LogFormat =1 #继续使用apache的默认格式 AllowToUpdateStatsFromBrowser=1 #允许浏览器刷新,njava的流量小,这个可以有 Include "/etc/awstats/awstats.www.njava.com.conf" #包含njava的配置
4 修改nginx的日志格式
bear@njava:/etc/awstats$sudo vi /etc/nginx/nginx.conf user www-data; worker_processes 2; error_log /var/log/nginx/error.log; pid /var/run/nginx.pid; events { worker_connections 1024; # multi_accept on; } http { include /etc/nginx/mime.types; #main 把nginx的输出日志定义成了apache格式的日志 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 /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; tcp_nodelay on; gzip on; gzip_disable "MSIE [1-6]\.(?!.*SV1)"; include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; }
4 修改awstats.www.njava.com.conf
bear@njava:/usr/local/awstats/tools$ sudo vi /etc/awstats/awstats.conf LogFile="/var/log/nginx/www.njava.access.log"
要看相应域名下的日志使用
http://awstats.njava.com/awstats.pl?config=cdn.njava.com
5 配置nginx
先确保了nginx已经代理了nginx-fcgi,可以参看 xxx
bear@njava:/etc/awstats$ vi /etc/nginx/sites-enabled/awstats.njava.com server { listen 80; server_name awstats.njava.com; access_log /var/log/nginx/awstats.log main; error_log /var/log/nginx/awstats_error.log; root /usr/local/awstats/wwwroot; #auth_basic "Restricted"; #auth_basic_user_file /etc/nginx/conf/awstats; 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_pass unix:/tmp/nginx-fcgi.sock; fastcgi_index awstats.pl; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location /icon { alias /usr/local/awstats/wwwroot/icon; location /icon { alias /usr/local/awstats/wwwroot/icon; } location /js { alias /usr/local/awstats/wwwroot/js; } location /css { alias /usr/local/awstats/wwwroot/css; } location /classes { alias /usr/local/awstats/wwwroot/classes; } }
为目录添加用户验证
bear@njava:/etc/nginx/conf$ sudo htpasswd -c awstats admin New password: Re-type new password: Adding password for user admin bear@njava:/etc/nginx/conf$
可以把/etc/nginx/sites-enabled/awstats.njava.com中出现的这个注释掉了
#auth_basic “Restricted”;
#auth_basic_user_file /etc/nginx/conf/awstats;