且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

AWstats 分析Nginx访问日志

更新时间:2022-06-27 06:30:05

软件下载地址:http://awstats.sourceforge.net/
我下载的是稳定版本awstats-6.95.tar.gz

先上传下载文件到服务器上我习惯放在/opt下面

 一、解压软件包:

tar zxvf  awstats-6.95.tar.gz
然后放到/usr/local/下面
mv awstats-6.95 /usr/local/awstats
二、接着执行 tools 目录中的 awstats_configure.pl 配置向导,创建一个新的统计
[root@www tools]# perl awstats_configure.pl

 ----- 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日志

 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  # 没有发现web 配置文件创建一个 选择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.test.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):
 # 默认的配置文件路径/etc/awstats回车即可

-----> Create config file '/etc/awstats/awstats.www.test.com.conf'
 Config file /etc/awstats/awstats.www.test.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.test.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.test.com.conf
You should have a look inside to check and change manually main parameters.
You can then manually update your statistics for 'www.test.com' with command:
> perl awstats.pl -update -config=www.test.com
You can also build static report pages for 'www.test.com' with command:
> perl awstats.pl -output=pagetype -config=www.test.com
Press ENTER to finish...
安装配置完成

 
三、编辑配置文件/etc/awstats/awstats.www.test.com.conf

找到LogFile="/var/log/httpd/mylog.log"
修改成nginx日志文件的正确位置
LogFile="/var/log/nginx/test.com.acc.log"
保存,退出。

四、修改Nginx 日志文件生成格式一适应awstats的需要
标红部分 注意:格式一定要注意,不能多一个空格,或少一个空格,否则在后面更新的时候会报告日志格式错误(这个问题浪费了我一天的时间)
http {
        client_header_buffer_size 16k;
    large_client_header_buffers 4 64k;
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

                      '$status $body_bytes_sent "$http_referer" '

                      '"$http_user_agent" $http_x_forwarded_for';     
               

 server {
    listen       80;
        server_name  www.test.com;
        access_log  /var/log/nginx/test.com.acc.log main;


保存后重启Nginx


由于在配置日志分析之前网站已经运行了一段时间,所以我得把线有的日志给清除掉,因为格式不匹配

 Cat /dev/null  > /var/log/nginx/test.com.acc.log

 现在多刷几次网站有了日志记录就行了

五、现在手动运行统计脚本:(添加到cron 任务中)
[root@www cgi-bin]#perl /usr/local/awstats/wwwroot/cgi-bin/awstats.pl  -update -config=www.test.com

Create/Update database for config "/etc/awstats/awstats.www.test.com.conf" by AWStats version 6.95 (build 1.943)

From data in log file "/var/log/nginx/test.com.acc.log "...

Phase 1 : First bypass old records, searching new record...

Direct access after last parsed record (after line 1348)
Jumped lines in file: 1348
 Found 1348 already parsed records.
Parsed lines in file: 798
 Found 0 dropped records,
 Found 9 corrupted records,
 Found 0 old records,
 Found 789 new qualified records.
 出现上面的输出,表示完全没问题可以继续。
如果Nginx日志格式不对就会报错:如下:

This means each line in your web server log file need to have "combined log format" like this:
111.22.33.44 - - [10/Jan/2001:02:14:14 +0200] "GET / HTTP/1.1" 200 1234 "http://www.fromserver.com/from.htm" "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"
And this is an example of records AWStats found in your log file (the record number 50 in your log):
220.181.27.12 - - [08/Jul/2010:04:06:32 +0800] HEAD /yiyou/27/pages/2930.swf HTTP/1.0 "200" 0 "-" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)" "-"
Setup ('/etc/awstats/awstats.www.test.com.conf' file, web server or permissions) may be wrong.
Check config file, permissions and AWStats documentation (in 'docs' directory).


六、都说nginx对cgi支持不是很好,我在此也就不写关于cgi的配置了

Awstats可以输出静态html格式的文件,而且静态页面还是很低碳的啊,哈哈! (添加到计划任务中)
[root@wwwcgi-bin]#perl/usr/local/awstats/tools/awstats_buildstaticpages.pl -update -config=www.test.com  -lang=cn -dir=/var/www/html -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl
#这个脚本命令就是输出静态格式的html 页面 -lang=cn语言,当然是中文了,-dir=/var/www/html 输出文件的路径。


Launch update process : "/usr/local/awstats/wwwroot/cgi-bin/awstats.pl" -config=www.test.com -update -configdir=
Build main page: "/usr/local/awstats/wwwroot/cgi-bin/awstats.pl" -config=www.test.com -staticlinks -lang=cn -output
Build alldomains page: "/usr/local/awstats/wwwroot/cgi-bin/awstats.pl" -config=www.test.com -staticlinks -lang=cn -output=alldomains
Build allhosts page: "/usr/local/awstats/wwwroot/cgi-bin/awstats.pl" -config=www.test.com -staticlinks -lang=cn -output=allhosts

………忽略后面输出

 
这时候可以配置Nginx访问那些静态页面了
http://www.test.com/awstats.www.test.com.html
访问认证的配置网上也有很多,在这里就免去了AWstats 分析Nginx访问日志
到这里也就差不多完事了!

 
七、在网上看到了一篇关于显示IP地址地区的配置,在这次我也添加进去了,还不错! :)

(感谢那些为互联网做出贡献的人!感谢这位作者!)

具体方法:

1. 首先下载插件(qqhostinfo.pm)。把它放在、/usr/local/awstats/wwwroot/cgi-bin/plugins中。
2. 下载分析QQWry.dat的perl脚本(qqwry.pl)。放在同一目录。
3. 修改,qqwry.pl中:
./QQWry.Dat 修改为 ${DIR}/plugins/QQWry.Dat
4. 去网上下载QQWry.Dat放在同一目录中 下载(http://www.cz88.net/fox/),注意文件名一定要是QQWry.Dat
5. 编辑你得awstats配置文件,大概在LoadPlugin="hostinfo"的后面增加一行
LoadPlugin="qqhostinfo"
重新运行上面第五、六步。
 

AWstats 分析Nginx访问日志

 


常见错误解决:

一、AWStats搜索关键词中文出现乱码解决办法

1. 修改站点配置文件awstats.www.test.com.conf
   把LoadPlugin=”decodeutfkeys”前边的#去掉

2. 安装Perl模块

二、[root@www nginx]#cpan
     cpan> install Encode
     cpan> install URI::Escape

重新运行上面第五、六步。

本文完!




     本文转自andylhz 51CTO博客,原文链接:http://blog.51cto.com/andylhz2009/346242,如需转载请自行联系原作者