当前位置:Gxlcms > mysql > awstats统计分析nginx日志

awstats统计分析nginx日志

时间:2021-07-01 10:21:17 帮助过:31人阅读

#修改nginx日志格式: log_format awstats '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/live.yofogo.com.access.log awstats; #

#修改nginx日志格式:
  log_format awstats '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
  access_log logs/live.yofogo.com.access.log awstats;

#Nginx切割日志脚本logcron.sh:
  mv /opt/nginx/nginx-1.2.1/logs/live.yofogo.com.access.log /opt/nginx/nginx-1.2.1/logs/live.yofogo.com.access_`date +%Y%m%d`.log
  killall -s USR1 nginx #使用USR1参数通知Nginx进程切换日志文件

#安装awstats
  wget http://prdownloads.sourceforge.net/awstats/awstats-7.0.tar.gz
  tar xzf awstats-7.0.tar.gz
  mv awstats-7.0 /usr/local/awstats
  cd /usr/local/awstats/tools
chmod +x /usr/local/awstats/tools/*.pl
chmod +x wwwroot/cgi-bin/*.pl
  mkdir /etc/awstats /var/lib/awstats

#awstats配置
  cd /usr/local/awstats/tools
  perl awstats_configure.pl:
    -----> 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 回车#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:
    #> live.yofogo.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):
    #> 回车

    ----> 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=live.yofogo.com
#回头把该命令填入crontab 按指定时间执行
    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.live.yofogo.com.conf
#新配置文件所在的路径
    You should have a look inside to check and change manually main parameters.
    You can then manually update your statistics for 'live.yofogo.com' with command:
    > perl awstats.pl -update -config=live.yofogo.com
    You can also build static report pages for 'live.yofogo.com' with command:
    > perl awstats.pl -output=pagetype -config=live.yofogo.com
    Press ENTER to finish...回车

  #编辑awstats配置文件:sudo vim /etc/awstats/awstats.live.yofogo.com.conf
LogFile="/opt/nginx/nginx-1.2.1/logs/live.yofogo.com.access_%YYYY-0%MM-0%DD-0.log" //统计的日志文件的路径
LogFormat=1//日志格式,这个一定要跟要分析的日志格式匹配,不然可能分析不出来数据或者分析出来的数据不正确
LogSeparator=" "//指定日志的分割符
SiteDomain="www.lme.pw"//要分析的网站域名
DirData="/opt/awstats"//分析完的数据文件存在的位置
Lang为Lang="cn"
#对应Nginx日志切割所生成的目录存放结构,要注意Awstats的年月日格式的跟Nginx的写法不同。现在执行统计的顺序是:
      Nginx 产生日志 –> 日志切割 –> Nginx 继续产生日志 –> 另存切割日志 –> 交由Awstats统计 –> 生成结果

#awstats静态文件统计资源配置
  mkdir -p /home/works/yofoto/awstats-web/awstats
  #cp -R /usr/local/awstats/wwwroot/css /home/works/yofoto/awstats-web
  #cp -R /usr/local/awstats/wwwroot/icon /home/works/yofoto/awstats-web
  #nginx映射:
    location ~ ^/awstats/ {# html 静态页面目录
root /home/works/yofoto/awstats-web;
index index.html;
access_log off;
error_log off;
charset gb2312; #最好把默认编码改成 gb2312避免浏览器因自动编码出现乱码的情况
}

location ~ ^/icon/ {# 图标目录
root /usr/local/awstats/wwwroot;
index index.html;
access_log off;
error_log off;
charset gb2312;
}
  #更新日志统计数据库及生成静态文件到指定目录
    /usr/local/awstats/tools/awstats_buildstaticpages.pl -update \
  -config=live.yofogo.com -lang=cn -dir=/home/works/yofoto/awstats-web/awstats \
  -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl

  #上述命令的具体意思如下:
    /usr/local/awstats/tools/awstats_buildstaticpages.pl Awstats 静态页面生成工具
    -update -config=live.yofogo.com 更新配置项
    -lang=cn 语言为中文
    -dir=/home/works/yofoto/awstats-web/awstats 统计结果输出目录
    -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl Awstats日志更新程序路径

#浏览器查看统计结果 http://youhostname/awstats/awstats.live.yofogo.com.html

#配置Awstats自动日志统计-设置crontab计划任务:
  sudo vim /etc/crontab
    #下午15:00进行日志切割
    15 00 * * * /opt/nginx/nginx-1.2.1/sbin/logcron.sh
    #下午15:20进行日志分析
    15 20 * * * /usr/local/awstats/tools/awstats_buildstaticpages.pl -update \
     -config=live.yofogo.com -lang=cn -dir=/home/works/yofoto/awstats-web/awstats \
     -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl
  #crontab /etc/crontab 指定cron所执行的配置档路径

#保护日志统计结果页面

用htpasswd生成密钥,

location ~ ^/awstats/ {# html 静态页面目录
root /home/works/yofoto/awstats-web;
index index.html;
access_log off;
error_log off;
charset gb2312;
auth_basic "admin";
auth_basic_user_file /home/works/yofoto/awstats-web/passwd.pass;
}

检测配置/nginx/sbin/nginx -t -c /nginx/conf/nginx.conf
执行命令killall –s HUP nginx 让 Nginx重新加载配置即可
清除80端口的进程: lsof -i :80|grep -v "PID"|awk '{print "kill -9",$2}'|sh

参考:
http://www.ibm.com/developerworks/cn/linux/l-cn-awstats-nginx
http://www.linuxyan.com/web-server/102.html
http://blog.rebill.info/archives/statistical-analysis-of-user-behavior-system-architecture-design.html

人气教程排行