Awstats分析Nginx日志

awstats 是一款日志统计工具,它使用 Perl 语言编写,可统计的日志类型包括 appache,nginx,ftp,mail 等,awstats 对 nginx 日志统计非常详细,如统计项

  • 按参观时间: 按月历史统计 按日期统计 按星期 每小时浏览次数
  • 按参观者: 国家或地区 城市 IP 最近参观日期 无法反解译的 IP 地址 搜索引擎网站的机器人
  • 浏览器统计: 每次参观所花时间 文件类别 下载 存取次数 入站处 出站处 操作系统版本 浏览器版本
  • 反向链接: 来源网址 由那些搜索引擎转介
  • HTTP: 错误码 错误次数 (400) 错误次数 (403) 错误次数 (404)
  • 搜索: 用以搜索的短语 用以搜索的关键词

初体验

安装

yum install epel
yum install awstats

创建配置

/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/目录下,修改该配置文件的日志位置

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统计 –> 生成结果

# 指定解析日志格式
LogFormat= "%time1 %host %other %other %refererquot %methodurlnoprot %code %bytesd %other %other %uaquot %otherquot"

开始分析日志

mkdir -p /var/lib/awstats # 分析结果会保存在该目录下
然后运行awstats的wwwroot目录中的awatsts.pl来测试一下
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=nginx

生成静态文件

# 创建保存静态文件目录
mkdir -p /home/www/awstats
# 生成文件命令
/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

修改 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;
}

参考:

0%