osnosn

  博客园 :: 首页 :: 博问 :: 闪存 :: :: 联系 :: 订阅 订阅 :: 管理 ::

awstats_yum_dnf_centos8_nginx_X-Forwarded-For

转载注明来源: 本文链接 来自osnosn的博客,写于 2020-05-26.

参考:
AWStats Other utilities
AWStats简介
还有另外一个日志分析工具叫 GoAccess

安装,用 yum 或 dnf

  • dnf install awstats
    centos8中,安装的是 7.7 版
    centos7中,安装的也是 7.7 版,以下内容也合适centos7。

修改 /etc/awstats/awstats.localhost.localdomain.conf

  • 读取多个log文件。
    LogFile="/usr/share/awstats/tools/logresolvemerge.pl /var/log/nginx/access.log /var/log/nginx/ssl.access.log |"
  • awstats 不直接支持读取 X-Forwarded-for: 这个记录参数。需用 extraX 参数来解决。
  • SiteDomain="12.34.56.78" 或者写上你的域名。
  • 按需修改
    • HostAliases="localhost 127.0.0.1"
    • SkipHosts="127.0.0.1"
    • SkipFiles=""
  • 改名 mv awstats.localhost.localdomain.conf awstats.12.34.56.78.conf
  • /etc/awstats/awstats.model.conf 这个文件不要动,只是个模板。awstats实际读取配置文件时,会忽略这个文件名。
  • awstats_configure.pl 不太合适在centos8中使用,它会把东西都copy到 /usr/local/awstats目录中。
  • 创建新的配置文件:cp awstats.model.conf awstats.mysite.conf , 然后修改 awstats.mysite.conf 文件。

生成静态文件

  • 用 crontab 执行 03 03 * * * /usr/share/awstats/tools/awstats_buildstaticpages.pl -config=12.34.56.78 -lang=cn2 -awstatsprog=/usr/share/awstats/wwwroot/cgi-bin/awstats.pl -dir=/your_web_path/html/stat/awstats/

    我的nginx document_root 是 /your_web_path/html

  • 上一行(crontab)中,可以用-lang=en
  • -lang=cn的话,生成的html是gbk编码的。改 utf-8
    • cd /usr/share/awstats/lang/ , iconv -f gbk -t utf-8 awstats-cn.txt > awstats-cn2.txt ,
      然后修改 awstats-cn2.txt 的第一行 PageCode=GBKPageCode=UTF-8 即可。

nginx 配置,访问 awstats 的静态文件。

参考安装包提供的范例 /usr/share/awstats/tools/nginx/awstats-nginx.conf

  • .htaccess.pw , 用 htpasswd -c .htaccess.pw username 指令创建。 需安装 yum install httpd-tools
location /awstatsicons/ {   #此项必须有
   alias   /usr/share/awstats/wwwroot/icon/;
}

location ^~ /stat/ {
   # 如果需要账号保护
   # auth_basic  "admin";
   # auth_basic_user_file   /your_web_path/html/stat/.htaccess.pw ;
   include /etc/nginx/default.d/php.conf;

   location /stat/awstats/classes/ {    #此项似乎没用
      alias   /usr/share/awstats/wwwroot/classes/;
   }
   location /stat/awstats/css/ {    #此项似乎没用
      alias   /usr/share/awstats/wwwroot/css/;
   }
   location /stat/awstats/js/ {    #此项似乎没用
      alias   /usr/share/awstats/wwwroot/js/;
   }
   location ~ /\.ht {
      deny all;
   }
   #动态显示awstats
   #location ~ ^/stat/awstats/cgi/(awredir|awstats)\.pl {
   #   fastcgi_param SCRIPT_FILENAME /usr/share/awstats/tools/nginx/awstats-fcgi.php;
   #   fastcgi_param X_SCRIPT_FILENAME $document_root$fastcgi_script_name;
   #   fastcgi_param X_SCRIPT_NAME $fastcgi_script_name;
   #   include fastcgi_params;
   #   #fastcgi_pass 127.0.0.1:9000;  #centos7,或者根据你的php-fpm的设置
   #   fastcgi_pass php-fpm;       #centos8
   #}
   # 如果使用了qqwry的IP插件 
   #location ~ ^/stat/awstats/cgi/qqwry\.pl {
   #   deny all;
   #}
}

访问 http://12.34.56.78/stat/awstats/awstats.12.34.56.78.html 就能看到了。

不生成静态文件。直接配置nginx的动态页面。

参考安装包提供的范例 /usr/share/awstats/tools/nginx/awstats-nginx.conf

  • cp /usr/share/awstats/wwwroot/cgi-bin/awstats.pl /your_web_path/html/stat/awstats/cgi/
    如果有qqwry插件cp /usr/share/awstats/wwwroot/cgi-bin/qqwry.pl /your_web_path/html/stat/awstats/cgi/
  • 把上面的nginx配置中的 # 部分,去掉注释。
  • 如果没用错误的话,service nginx reload
  • 访问 http://12.34.56.78/stat/awstats/cgi/awstats.pl?config=12.34.56.78 就能看到了。

如果出现错误:
Error: Access to statistics is only allowed from an authenticated session to authenticated users
则检查 /etc/awstats/awstat.xxxx.conf 中 AllowAccessFromWebToAuthenticatedUsersOnly=0

如果要统计用户的访问

  • 改这个 ShowAuthenticatedUsers=UVPHBL

统计 X-Forwarded-For, 来自反向代理的访问IP

另一个解决办法,用 %host_proxy 并且要修改源代码。

  • 首先 nginx 的 log_format 要加入 x-forwarded-for
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;  #指定记录格式
  • awstats 的配置文件,需要修改一项
    • LogFormat = 1 改为
      LogFormat = "%host %other %logname %time1 %methodurl %code %bytesd %refererquot %uaquot %extra1"
    • 增加 extra1 对应的配置。
ExtraSectionName1="X-Forwarded-For"
ExtraSectionCodeFilter1="200 304"
ExtraSectionCondition1=""
ExtraSectionFirstColumnTitle1="X-Forwarded-For"
ExtraSectionFirstColumnValues1="extra1,([^&]+)"
ExtraSectionFirstColumnFormat1="%s"
ExtraSectionStatTypes1=UVPHBL
ExtraSectionAddAverageRow1=0
ExtraSectionAddSumRow1=1
MaxNbOfExtra1=20
MinHitExtra1=1
  • 注,extra 要按顺序从 extra1 开始,不能从 extra2 开始。

转载注明来源: 本文链接 来自osnosn的博客.

posted on 2020-05-26 23:23  osnosn  阅读(302)  评论(0编辑  收藏  举报