AlpineLinux 使用 goaccess 分析 nginx 日志

warning: 这篇文章距离上次修改已过225天,其中的内容可能已经有所变动。

此脚本可以在alpinelinux上直接运行,如果你是其他操作系统,请适当修改后执行。

脚本依次执行如下操作
0、goaccessnginx 变量对照表
1、安装 goaccess 软件包
2、配置 goaccess 参数
3、配置 nginx 日志
4、下载 geoip 数据库
5、启动 goaccess 服务

#!/bin/sh
#
# Author: wang@rehiy.com
# Refer : https://www.rehiy.com/post/291
#
# Nginx Log Conversion table:
#   $time_local         %d:%t %^
#   $host               %v
#   $http_host          %v
#   $remote_addr        %h
#   $request_time       %T
#   $request_method     %m
#   $request_uri        %U
#   $server_protocol    %H
#   $request            %r
#   $status             %s
#   $body_bytes_sent    %b
#   $bytes_sent         %b
#   $http_referer       %R
#   $http_user_agent    %u
#

#################################################

if ! type goaccess >/dev/null; then
  apk add goaccess
fi

##########################################

cat <<EOF >/etc/goaccess/goaccess.conf 
time-format %T
date-format %d/%b/%Y
log_format %h - %e [%d:%t %^] "%r" %s %b "%R" "%u" "%^" %^ %^ %^ %T
geoip-database /usr/share/geoip/GeoIPCity.dat
ws-url wss://goac.example.org:443/report/d
EOF

##########################################

sed -i 's/  access_log off/# access_log off/' /etc/nginx/nginx.conf

cat <<EOF >/etc/nginx/http.d/access-log.conf
log_format access
    '\$remote_addr - \$remote_user [\$time_local] "\$request" '
    '\$status \$body_bytes_sent "\$http_referer" "\$http_user_agent" "\$http_x_forwarded_for" '
    '\$connection \$upstream_addr \$upstream_response_time \$request_time'
;
access_log /var/log/nginx/access.log access;
EOF

##########################################

cd /usr/share/geoip/

wget https://mailfud.org/geoip-legacy/GeoIP.dat.gz
wget https://mailfud.org/geoip-legacy/GeoIPv6.dat.gz
wget https://mailfud.org/geoip-legacy/GeoIPCity.dat.gz
wget https://mailfud.org/geoip-legacy/GeoIPCityv6.dat.gz

gunzip *.gz

cd /tmp

#################################################

odir=/var/www/default/net.jidns.spry/report

mkdir -p $odir $odir/archive

goaccess /var/log/nginx/access.log \
    -o $odir/index.html \
    --real-time-html \
    --daemonize \

正文完