Nginx配置Awstats分析Nginx日志笔记

简介:

1、修改Nginx日志格式:

1
2
3
4
5
log_format json  '$remote_addr - $remote_user [$time_local] "$request" '
              '$status $body_bytes_sent "$http_referer" '
              '"$http_user_agent" "$http_x_forwarded_for"' ;
              
access_log  /data/nginx_logs/access .log json;


2、Nginx日志切割(shell脚本,略)


3、安装GeoIP库

1
yum -y  install  GeoIP GeoIP-devel perl-Geo-IP


4、安装Awstats

1
2
3
4
5
6
tar  xvf awstats-7.4. tar .gz
mv  awstats-7.4  /usr/local/awstats
cd  /usr/local/
chown  root:root -R awstats/
chmod  +x  /usr/local/awstats/tools/ *.pl
chmod  +x  /usr/local/awstats/wwwroot/cgi-bin/ *.pl


5、运行脚本生成配置

1
2
cd  /usr/local/awstats/tools/
. /awstats_configure .pl

脚本交互1:

1
2
Config  file  path ( 'none'  to skip web server setup):
因为在此我们使用的是nginx,所以填写none

脚本交互2:

1
2
3
4
Do you want me to build a new AWStats config /profile  
file  (required  if  first  install ) [y /N ]
 
输入Y创建一个新的统计配置文件。

脚本交互3:

1
2
3
Your web site, virtual server or profile name:  
> www. test .com 
在这输入自己的网站域名

脚本交互4:

1
2
3
Directory path to store config  file (s) (Enter  for  default):  
使用默认配置,生成配置文件

后面的直接按回车就可以


6、修改上面生成的配置文件/etc/awstats/awstats.app.mir.6wtx.com.conf

1
2
3
4
LogFile=" /data/nginx_logs/cut_logs/ %YYYY-24%MM-24%DD-24/
DirData= "/data/awstats" 
LoadPlugin= "geoip GEOIP_STANDARD /usr/share/GeoIP/GeoIP.dat"
LoadPlugin= "geoip_city_maxmind GEOIP_STANDARD /usr/share/GeoIP/GeoLiteCity.dat"


7、生成数据文件并配置Nginx,此处应该有两种办法:

一种是用fastcgi调用perl分析数据文件

一种是直接生成静态文件nginx解析


第一种办法:

7.1.1、安装FCGI和FCGI::ProcManager

1
2
cpan> install  FCGI
cpan> install  FCGI::ProcManager

7.1.2、创建fastcgi来执行perl:/usr/local/nginx/sbin/fcgi

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/perl
use FCGI;
#perl -MCPAN -e 'install FCGI'
use Socket;
use POSIX qw(setsid);
#use Fcntl;
require  'syscall.ph' ;
&daemonize;
#this keeps the program alive or something after exec'ing perl scripts
END() { } BEGIN() { }
*CORE::GLOBAL:: exit  = sub { die  "fakeexit\nrc=" . shift (). "\n" ; };
eval  q{ exit };
if  ($@) {
         exit  unless $@ =~ /^fakeexit/;
};
&main;
sub daemonize() {
     chdir  '/'                  or die  "Can't chdir to /: $!" ;
     defined(my $pid = fork)   or die  "Can't fork: $!" ;
     exit  if  $pid;
     setsid                    or die  "Can't start a new session: $!" ;
     umask  0;
}
sub main {
         #$socket = FCGI::OpenSocket( "127.0.0.1:8999", 10 );
         $socket = FCGI::OpenSocket(  "/usr/local/nginx/fastcgi_temp/perl_cgi-dispatch.sock" , 10 );
#use UNIX sockets - user running this script must have w access to the 'nginx' folder!!
         $request = FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%req_params, $socket );
         if  ($request) { request_loop()};
             FCGI::CloseSocket( $socket );
}
sub request_loop {
         while ( $request->Accept() >= 0 ) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
            #processing any STDIN input from WebServer (for CGI-POST actions)
            $stdin_passthrough = '' ;
            $req_len = 0 + $req_params{ 'CONTENT_LENGTH' };
            if  (($req_params{ 'REQUEST_METHOD' eq  'POST' ) && ($req_len != 0) ){
                 my $bytes_read = 0;
                 while  ($bytes_read < $req_len) {
                         my $data =  '' ;
                         my $bytes =  read (STDIN, $data, ($req_len - $bytes_read));
                         last  if  ($bytes == 0 || !defined($bytes));
                         $stdin_passthrough .= $data;
                         $bytes_read += $bytes;
                 }
             }
             #running the cgi app
             if  ( (-x $req_params{SCRIPT_FILENAME}) &&  #can I execute this?
                  (-s $req_params{SCRIPT_FILENAME}) &&  #Is this file empty?
                  (-r $req_params{SCRIPT_FILENAME})      #can I read this file?
             ){
                 pipe(CHILD_RD, PARENT_WR);
                 my $pid =  open (KID_TO_READ,  "-|" );
                 unless(defined($pid)) {
                         print( "Content-type: text/plain\r\n\r\n" );
                         print "Error: CGI app returned no output - Executing $req_params
{SCRIPT_FILENAME} failed !\n";
                         next;
                 }
                 if  ($pid > 0) {
                         close(CHILD_RD);
                         print PARENT_WR $stdin_passthrough;
                         close(PARENT_WR);
                         while (my $s = <KID_TO_READ>) { print $s; }
                         close KID_TO_READ;
                         waitpid($pid, 0);
                 else  {
                         foreach $key ( keys %req_params){
                            $ENV{$key} = $req_params{$key};
                         }
                         # cd to the script's local directory
                         if  ($req_params{SCRIPT_FILENAME} =~ /^(.*)\/[^\/]+$/) {
                                 chdir $1;
                         }
                         close(PARENT_WR);
                         close(STDIN);
                         #fcntl(CHILD_RD, F_DUPFD, 0);
                         syscall(&SYS_dup2, fileno(CHILD_RD), 0);
                         #open(STDIN, "<&CHILD_RD");
                         exec ($req_params{SCRIPT_FILENAME});
                         die( "exec failed" );
                 }
             }
             else  {
                 print( "Content-type: text/plain\r\n\r\n" );
                 print "Error: No such CGI app - $req_params{SCRIPT_FILENAME} may not exist or is
not executable by this process.\n";
             }
         }
}

7.1.3、授权:

1
chmod  755  /usr/local/nginx/sbin/fcgi

7.1.4、启动:

1
perl  /usr/local/nginx/sbin/fcgi  > /dev/null  2>&1

7.1.5、授权socker让Nginx调用:

1
chown  www:www  /usr/local/nginx/fastcgi_temp/perl_cgi-dispatch .sock

7.1.6、创建/usr/local/nginx/conf/fastcgi_params1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param QUERY_STRING     $query_string;
                fastcgi_param REQUEST_METHOD   $request_method;
                fastcgi_param CONTENT_TYPE     $content_type;
                fastcgi_param CONTENT_LENGTH   $content_length;
                fastcgi_param GATEWAY_INTERFACE CGI /1 .1;
                fastcgi_param SERVER_SOFTWARE    nginx;
                fastcgi_param SCRIPT_NAME        $fastcgi_script_name;
                fastcgi_param REQUEST_URI        $request_uri;
                fastcgi_param DOCUMENT_URI       $document_uri;
                fastcgi_param DOCUMENT_ROOT      $document_root;
                fastcgi_param SERVER_PROTOCOL    $server_protocol;
                fastcgi_param REMOTE_ADDR        $remote_addr;
                fastcgi_param REMOTE_PORT        $remote_port;
                fastcgi_param SERVER_ADDR        $server_addr;
                fastcgi_param SERVER_PORT        $server_port;
                fastcgi_param SERVER_NAME        $server_name;
                fastcgi_read_timeout 60;

7.1.7、Nginx增加虚拟主机:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
server {
                 listen      33333;
                 server_name  xxxxxxxxxxx;
  
                  location / {
                         root    /data/awstats ;
                         index  index.html index.htm;
                 }
  
  
                 location ~* ^ /cgi-bin/ .*\.pl$ {
                         root  /usr/local/awstats/wwwroot ;
                         fastcgi_pass unix: /usr/local/nginx/fastcgi_temp/perl_cgi-dispatch .sock;
                         fastcgi_index index.pl;
                         include  fastcgi_params1;
                         charset gb2312;
                 }
                 location ~ ^ /icon/  {         # 图标目录
                         root    /usr/local/awstats/wwwroot ;
                         index  index.html;
                         access_log off;
                         error_log off;
                 }
         }

最后重启Nginx


7.1.8、生成awstats数据文件:

1
/usr/local/awstats/wwwroot/cgi-bin/awstats .pl -update -config=www. test .com


7.1.9、打开URL查看效果:http://xxxxxxxxx:33333/cgi-bin/awstats.pl?config=www.test.com

wKiom1cR4rzxS29dAADm-KvCiOQ099.png


第二种办法:

7.2.1、配置Nginx,不需要调用perl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
server {
                 listen       44444;
                 server_name  xxxxxxxxxxx;
                 root    /data/awstats ;
                 index  index.html;
                 access_log off;
                 error_log off;
                 charset gb2312;
 
                 location ~ ^ /icon/  {         # 图标目录
                         root    /usr/local/awstats/wwwroot ;
                         index  index.html;
                         access_log off;
                         error_log off;
                 }
         }

重启Nginx

7.2.2、生成awstats静态数据文件:

1
/usr/local/awstats/tools/awstats_buildstaticpages .pl -update -config=www. test .com -lang=cn - dir = /data/awstats  -awstatsprog= /usr/local/awstats/wwwroot/cgi-bin/awstats .pl

7.2.3、 打开URL查看效果:http://xxxxxxxxxx:44444/awstats.www.test.com.html

wKioL1cR42mA37REAAD25PNqsss811.png


8、添加定时任务:

1
2
5 0 * * *  /usr/local/awstats/wwwroot/cgi-bin/awstats .pl -update -config=www.sunsky.com > /dev/null  2>&1
10 0 * * *  /usr/local/awstats/tools/awstats_buildstaticpages .pl -update -config=www. test .com -lang=cn - dir = /data/awstats  -awstatsprog= /usr/local/awstats/wwwroot/cgi-bin/awstats .pl > /dev/null  2>&1
本文转自运维笔记博客51CTO博客,原文链接 http://blog.51cto.com/lihuipeng/1764467如需转载请自行联系原作者

lihuipeng
相关实践学习
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
相关文章
|
2月前
|
监控 安全 程序员
Python日志模块配置:从print到logging的优雅升级指南
从 `print` 到 `logging` 是 Python 开发的必经之路。`print` 调试简单却难维护,日志混乱、无法分级、缺乏上下文;而 `logging` 支持级别控制、多输出、结构化记录,助力项目可维护性升级。本文详解痛点、优势、迁移方案与最佳实践,助你构建专业日志系统,让程序“有记忆”。
268 0
|
3月前
|
监控 安全 搜索推荐
使用EventLog Analyzer进行日志取证分析
EventLog Analyzer助力企业通过集中采集、归档与分析系统日志及syslog,快速构建“数字犯罪现场”,精准追溯安全事件根源。其强大搜索功能可秒级定位入侵时间、人员与路径,生成合规与取证报表,确保日志安全防篡改,大幅提升调查效率,为执法提供有力证据支持。
166 0
|
3月前
|
缓存 Java 应用服务中间件
Spring Boot配置优化:Tomcat+数据库+缓存+日志,全场景教程
本文详解Spring Boot十大核心配置优化技巧,涵盖Tomcat连接池、数据库连接池、Jackson时区、日志管理、缓存策略、异步线程池等关键配置,结合代码示例与通俗解释,助你轻松掌握高并发场景下的性能调优方法,适用于实际项目落地。
624 5
|
安全 BI 网络安全
EventLog Analyzer 如何满足等保合规要求?密码有效期、产品日志保留、配置备份三大核心问题全面解答
EventLog Analyzer(ELA)助力企业满足网络安全等级保护要求,支持配置自动/手动备份、日志180天留存及密码策略管理,提升合规性与安全运营效率。
130 0
|
5月前
|
JSON 安全 Go
Go语言项目工程化 —— 日志、配置、错误处理规范
本章详解Go语言项目工程化核心规范,涵盖日志、配置与错误处理三大关键领域。在日志方面,强调其在问题排查、性能优化和安全审计中的作用,推荐使用高性能结构化日志库zap,并介绍日志级别与结构化输出的最佳实践。配置管理部分讨论了配置分离的必要性,对比多种配置格式如JSON、YAML及环境变量,并提供viper库实现多环境配置的示例。错误处理部分阐述Go语言显式返回error的设计哲学,讲解标准处理方式、自定义错误类型、错误封装与堆栈追踪技巧,并提出按调用层级进行错误处理的建议。最后,总结各模块的工程化最佳实践,助力构建可维护、可观测且健壮的Go应用。
|
6月前
|
存储 NoSQL MongoDB
Docker中安装MongoDB并配置数据、日志、配置文件持久化。
现在,你有了一个运行在Docker中的MongoDB,它拥有自己的小空间,对高楼大厦的崩塌视而不见(会话丢失和数据不持久化的问题)。这个MongoDB的数据、日志、配置文件都会妥妥地保存在你为它精心准备的地方,天旋地转,它也不会失去一丁点儿宝贵的记忆(即使在容器重启后)。
732 4
|
5月前
|
监控 安全 NoSQL
【DevOps】Logstash详解:高效日志管理与分析工具
Logstash是ELK Stack核心组件之一,具备强大的日志收集、处理与转发能力。它支持多种数据来源,提供灵活的过滤、转换机制,并可通过插件扩展功能,广泛应用于系统日志分析、性能优化及安全合规等领域,是现代日志管理的关键工具。
821 0
|
应用服务中间件 Apache nginx
nginx自己写日志切割脚本
转自本人的博客:http://blog.teier.cn
1574 0
|
监控 应用服务中间件 nginx
|
应用服务中间件 nginx Perl