nginx教程五,用GoAccess工具实时查看nginx日志

一、安装GoAccess

 推荐的官方安装方式https://www.vultr.com/docs/how-to-install-goaccess-on-centos-7

1. 更新系统

yum -y install epel-release
yum -y update
shutdown -r now

2. 安装环境依赖,goaccess是C语言开发的,需要安装ncurses和gcc

yum -y install ncurses-devel gcc
yum -y install geoip-devel tokyocabinet-devel

3. 安装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 --prefix=/root/goaccess
make
make install

4. 加入到/usr/bin中,可以直接使用goaccess命令

ln -s /root/goaccess/bin/goaccess /usr/bin/goaccess
goaccess -V

二、配置nginx日志格式

1. nginx.conf配置文件主要如下

2. 启动nginx查看日志如下

tail -f nginx/logs/access.log

三、使用goaccess

1. 创建好存放goaccess生成报表的目录,如果不创建目录可能会报错

mkdir -p /root/goaccess/nginxhtml

2. 使用goaccess工具创建报表,注意最好使用绝对路径

goaccess -d /root/nginx/logs/access.log -o /root/goaccess/nginxhtml/report.html --real-time-html --time-format='%H:%M:%S' --date-format='%d/%b/%Y' --log-format=COMBINED &

补充:如果报错Unable to set bind: Address already in use,则可以通过netstat工具检查端口7890是否被占用,如果netstat没有安装需要执行 yum -y install net-tools

3. 根据工具指定的报表路径,配置nginx访问

四、goaccess自定义日志格式

1. goaccess日志格式与服务器log_format对应关系

%t 匹配time-format格式的时间字段

%d 匹配date-format格式的日期字段

%h host(客户端ip地址,包括ipv4和ipv6)

%r 来自客户端的请求行

%m 请求的方法

%U URL路径

%H 请求协议

%s 服务器响应的状态码

%b 服务器返回的内容大小

%R HTTP请求头的referer字段

%u 用户代理的HTTP请求报头

%D 请求所花费的时间,单位微秒

%T 请求所花费的时间,单位秒

%^ 忽略这一字段

2. 转换nginx中log_format到对应的goaccess日志

log_format main '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" $http_x_forwarded_for $request_time $upstream_response_time';

转换后

%h %^[%d:%t %^] "%r" %s %b "%R" "%u" %^ %^ %T
原文地址:https://www.cnblogs.com/hujiapeng/p/14405603.html