nginx+awstats 统计web服务器log的教程

发布时间:2019-07-26编辑:脚本学堂
默认编译安装的nginx对cgi的支持并不好(所以在编译的时候一般都没打开这个功能)

默认编译安装nginx对cgi的支持并不好(所以在编译的时候一般都没打开这个功能)
google了一把。大家都用fastcgi来支持。http://wiki.codemongers.com/NginxSimpleCGI
很多人懒得看英文,我就翻译一下吧。
如果你没有cgi的文件,可以自己建立一个简单的。
只需要如下三行代码就可以了。
 

复制代码 代码如下:

#vi index.cgi
#!/usr/bin/perl

print "Content-type: text/htmlnn";
print "<html><body>Hello, world.</body></html>";

放到web根目录下的cgi-bin目录里面,记得要给它执行权限
接下来是用perl写一个daemon程序来处理cgi文件,幸运的是源代码已经有了。嘿嘿
 

复制代码 代码如下:

#vi cgiwrap-fcgi.pl
 
#!/usr/bin/perl -w
use FCGI;
use Socket;
use FCGI::ProcManager;
sub shutdown { FCGI::CloseSocket($socket); exit; }
sub restart  { FCGI::CloseSocket($socket); &main; }
use sigtrap 'handler', &shutdown, 'normal-signals';
use sigtrap 'handler', &restart,  'HUP';
require 'syscall.ph';
use POSIX qw(setsid);

#&daemonize; we don't daemonize when running under runsv
#this keeps the program alive or something after exec'ing perl scripts
END()   { }
BEGIN() { }
{
    no warnings;
    *CORE::GLOBAL::exit = sub { die "fakeexitnrc=" . 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 ); #use IP sockets
#$socket = FCGI::OpenSocket( "/var/run/nginx/perl_cgi-dispatch.sock", 10 ); #use UNIX sockets - user running this script must have w access to the 'nginx' folder!!
#foreach $item (keys %ENV) { delete $ENV{$item}; }
    $proc_manager = FCGI::ProcManager->new( {n_processes => 5} );
    $socket = FCGI::OpenSocket( "/opt/nginx/fcgi/cgi.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,
        &FCGI::FAIL_ACCEPT_ON_INTR );
    $proc_manager->pm_manage();
    if ($request) { request_loop() }
    FCGI::CloseSocket($socket);
}

sub request_loop {
    while ( $request->Accept() >= 0 ) {
        $proc_manager->pm_pre_dispatch();

        #processing any STDIN input from WebServer (for CGI-POST actions)
        $stdin_passthrough = '';
        { no warnings; $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 );
            pipe( PARENT_ERR, CHILD_ERR );
            my $pid = open( CHILD_O, "-|" );
            unless ( defined($pid) ) {
                print("Content-type: text/plainrnrn");
                print
"Error: CGI app returned no output - Executing $req_params{SCRIPT_FILENAME} failed !n";
                next;
            }
            $oldfh = select(PARENT_ERR);
            $|     = 1;
            select(CHILD_O);
            $| = 1;
            select($oldfh);
            if ( $pid > 0 ) {
                close(CHILD_RD);
                close(CHILD_ERR);
                print PARENT_WR $stdin_passthrough;
                close(PARENT_WR);
                $rin = $rout = $ein = $eout = '';
                vec( $rin, fileno(CHILD_O),    1 ) = 1;
                vec( $rin, fileno(PARENT_ERR), 1 ) = 1;
                $ein    = $rin;
                $nfound = 0;

                while ( $nfound =
                    select( $rout = $rin, undef, $ein = $eout, 10 ) )
                {
                    die "$!" unless $nfound != -1;
                    $r1 = vec( $rout, fileno(PARENT_ERR), 1 ) == 1;
                    $r2 = vec( $rout, fileno(CHILD_O),    1 ) == 1;
                    $e1 = vec( $eout, fileno(PARENT_ERR), 1 ) == 1;
                    $e2 = vec( $eout, fileno(CHILD_O),    1 ) == 1;

                    if ($r1) {
                        while ( $bytes = read( PARENT_ERR, $errbytes, 4096 ) ) {
                            print STDERR $errbytes;
                        }
                        if ($!) {
                            $err = $!;
                            die $!;
                            vec( $rin, fileno(PARENT_ERR), 1 ) = 0
                                unless ( $err == EINTR or $err == EAGAIN );
                        }
                    }
                    if ($r2) {
                        while ( $bytes = read( CHILD_O, $s, 4096 ) ) {
                            print $s;
                        }
                        if ( !defined($bytes) ) {
                            $err = $!;
                            die $!;
                            vec( $rin, fileno(CHILD_O), 1 ) = 0
                                unless ( $err == EINTR or $err == EAGAIN );
                        }
                    }
                    last if ( $e1 || $e2 );
                }
                close CHILD_RD;
                close PARENT_ERR;
                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(PARENT_ERR);
                close(STDIN);
                close(STDERR);

                #fcntl(CHILD_RD, F_DUPFD, 0);
                syscall( &SYS_dup2, fileno(CHILD_RD),  0 );
                syscall( &SYS_dup2, fileno(CHILD_ERR), 2 );

                #open(STDIN, "<&CHILD_RD");
                exec( $req_params{SCRIPT_FILENAME} );
                die("exec failed");
            }
        } else {
            print("Content-type: text/plainrnrn");
            print
"Error: No such CGI app - $req_params{SCRIPT_FILENAME} may not exist or is not executable by this process.n";
        }
    }
}

这个文件只需要修改一个地方/opt/nginx/fcgi/cgi.sock,改到你想放的地方,比如/var/run/fcgi.sock 注意要有权限,以便nginix可以正常访问它,如果不知道怎么配置权限
就chmod 777 好了(汗,一个很烂的习惯),正确的做法是将该文件的属主和数组设为nginx的运行用户,
比如nginx用nobody用户运行,则 chown nobody:nobody /var/run/cgi.sock 注意,上层目录也要有执行权限,这样才能访问这个unix socket(访问不到目录,自然不能访问文件了)
这些都做好以后可以启动这个文件了,
#chmod 755 cgiwrap-fcgi.pl
#./cgiwrap-fcgi.pl &
后台运行。不过这该死的程序居然在终端打印日志,一会调试成功了,可以用
./cgiwrap-fcgi.pl >/dev/null 2>&1 &
这样世界就清净了
如果想保留日志,自己改perl代码吧,不是很难,加上输出文件句柄就可以了。保存到文件中。嘿嘿。
接下来就修改nginx.conf
  

复制代码 代码如下:
  location ~ ^/cgi-bin/.*.cgi$ {
        gzip off; #gzip makes scripts feel slower since they have to complete before getting gzipped
        fastcgi_pass  unix:/var/run/nginx/cgi.sock;
        fastcgi_index index.cgi;
        fastcgi_param SCRIPT_FILENAME /var/www/cgi-bin$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;

下面这么多其实可以用一个文件来包含起来,不用这么多,看的心乱
 

复制代码 代码如下:
        location ~ ^/cgi-bin/.*.cgi$ {
        gzip off; #gzip makes scripts feel slower since they have to complete before getting gzipped
        fastcgi_pass  unix:/var/run/nginx/cgi.sock;
        fastcgi_index index.cgi;
        include fastcgi_params;
        }

ok,接下来重启nginx
 kill -HUP `cat /usr/local/nginx/nginx.pid`
然后我们访问刚才的那个cgi文件,[url]http://XXXXXXXXXX/cgi-bin/index.cgi[/url]  如果看到hello world ,就说明成功了。嘿嘿,就这么简单。
 
awstats默认的配置中只有IIS和apache的LOG格式,需要我们自定义一下
在/etc/awstats/awstats.mysite.conf里设置log格式如下(默认值是1):
LogFormat="%host %other %logname %time1 %methodurl %code %bytesd %refererquot %uaquot %otherquot"
 
在nginx中也配置好log格式
 log_format  wwwlogs  '$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/blog_log.log  wwwlogs;
 
再设置log的轮转
 
3)新建一个日志回滚,vi /etc/logrotate.d/nginx (路径根据操作系统不同而变化)
内容如下:
 

复制代码 代码如下:
/var/log/nginx {
daily
missingok
rotate 7
compress
delaycompress notifempty
create 644 nginx root sharedscripts
prerotate
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl --config=www.mysite.com -update
endscript
postrotate
if [ -f /usr/local/nginx/nginx.pid ]; then
kill -USR1 `cat /usr/local/nginx/nginx.pid`
fi
endscript
}