GoAccess实现可视化进行access监控
- nginx的access.log来分析定位问题和分析用户运营的数据
- 文件在/var/log/nginx/access.log
- 难题在于实时分析,但goaccess可以。
goaccess介绍与安装步骤
- 可通过图形化方式,用过websocket协议,实时把access.log的变迁反应在浏览器中。
- GoAccess英文官方网站
安装 GoAccess 非常的容易。只需要下载、解压以及编译就好了:
$ wget http://tar.goaccess.io/goaccess-1.2.tar.gz
$ tar -xzvf goaccess-1.2.tar.gz
$ cd goaccess-1.2/
$ ./configure --enable-utf8 --enable-geoip=legacy
$ make
# make install
在执行./configure时,可能会有以下问题:
configure: error:
*** Missing development files for the GeoIP library
需执行以下步骤
$ wget https://github.com/maxmind/geoip-api-c/releases/download/v1.6.11/GeoIP-1.6.11.tar.gz
$ tar -xzvf GeoIP-1.6.11.tar.gz
$ cd GeoIP-1.6.11
$ ./configure
$ make
# make install
在执行./configure时,还可能遇到:
configure: error: *** Missing development libraries for ncursesw
要使用–enable-utf8,你需要安装ncursesw
sudo apt-get install libncursesw5-dev
产生webscoket服务观察access.log变化
- 最后就是在存放access.log目录下,本人文件位置在
/usr/local/openresty/nginx/logs
- 然后执行
sudo goaccess access.log -o ../html/report.html --real-time-html --time-format='%H:%M:%S' --date-format='%d/%b/%Y' --log-format=COMBINED
- 运行后会显示
WebSocket server ready to accept new client connections
在nginx.conf中添加以下引导文件
location /report.html{
alias /usr/local/openresty/nginx/html/report.html;
}
修改goaccess.log配置文件说明(可不改)
############################
/etc/nginx/nginx.conf
log_format main '$server_name $remote_addr - $remote_user
[$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'$upstream_addr $request_time $upstream_response_time'
############################
/usr/local/etc/goaccess.conf
log-format %^ %h %^ %^ [%d:%t %^] “%r”
%s %b “%R”
“%u” “%^”
%^ %T %^
#########################################
/usr/local/etc/goaccess.conf
$server_name %^ 忽略这一字段
$remote_addr - %h%^ host(客户端ip地址,包括ipv4和ipv6)
$remote_user %^ 忽略这一字段
[$time_local] [%d:%t %^] 匹配date-format格式的日期字段--
匹配time-format格式的时间字段--忽略这一字段
"$request" “%r” 来自客户端的请求行
$status %s 用户代理的HTTP请求报头
$body_bytes_sent %b 服务器返回的内容大小
$http_referer “%R” HTTP请求头的referer字段
$http_user_agent “%u” 用户代理的HTTP请求报头
$http_x_forwarded_for %^ 忽略这一字段
$upstream_addr %^ 忽略这一字段
$request_time %T 请求所花费的时间,单位秒
$upstream_response_time %^ 忽略这一字段
#########################################
%t 匹配time-format格式的时间字段
%D 请求所花费的时间,单位微秒
%H 请求协议
%U URL路径
%m 请求的方法
####################
###########################
'$remote_addr - $remote_user [$time_local] "$request"'
'$status $body_bytes_sent "$http_referer"'
'"$http_user_agent" "$http_x_forwarded_for"'
###############################################
%h%^%^[%d:%t %^] "%r" %s%b "%R" "%u" "%^"