中文网站
https://www.goaccess.cc/
英文网站
http://goaccess.io/
编译安装
#yum安装依赖包
yum install glib2 glib2-devel GeoIP-devel ncurses-devel zlib zlib-devel
yum install gcc -y
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm
yum -y install GeoIP-update
$ 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
#修改/etc/nginx/nginx.conf文件的日志存储格式
log_format main '$remote_addr - $remote_user [$time_local] requesthost:"$http_host"; "$request" requesttime:"$request_time"; '
'$status $body_bytes_sent "$http_referer" - $request_body'
'"$http_user_agent" "$http_x_forwarded_for"';
#修改文件/usr/local/etc/goaccess.conf改成goaccess格式标准对应为
time-format %T
date-format %d/%b/%Y
log-format %h - %^ [%d:%t %^] requesthost:"%v"; "%r" requesttime:"%T"; %s %b "%R" - %^"%u"
#测试生成页面
goaccess -f /var/log/nginx/access.log -c -a>/usr/share/nginx/html/go.html
#后台实时生成数据到goaccess页面
/usr/local/bin/goaccess /var/log/nginx/access.log -o /usr/share/nginx/html/go.html --real-time-html &
nginx默认日志格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
具体参考: https://nginx.org/en/docs/http/ngx_http_log_module.html#example
log_format格式变量:
$remote_addr #记录访问网站的客户端地址
$remote_user #远程客户端用户名
$time_local #记录访问时间与时区
$request #用户的http请求起始行信息
$status #http状态码,记录请求返回的状态码,例如:200、301、404等
$body_bytes_sent #服务器发送给客户端的响应body字节数
$http_referer #记录此次请求是从哪个连接访问过来的,可以根据该参数进行防盗链设置。
$http_user_agent #记录客户端访问信息,例如:浏览器、手机客户端等
$http_x_forwarded_for #当前端有代理服务器时,设置web节点记录客户端地址的配置,此参数生效的前提是代理服务器也要进行相关的x_forwarded_for设置
其他还有:
'"$upstream_addr" "$upstream_status" "$upstream_response_time" "$request_time"'
注意: 仅仅需要依赖 ncurses 模块。
提示: 如果需要使用实时 HTML 报告, 请确保开放 7890 端口。详见这里。
配置选项
GoAccess 拥有多个配置选项。获取完整的最新配置选项列表,请运行:./configure --help
--enable-debug
使用调试标志编译且关闭编译器优化。
--enable-utf8
宽字符支持。依赖 Ncursesw 模块。
--enable-geoip=<legacy|mmdb>
地理位置支持。依赖 MaxMind GeoIP 模块。legacy 将使用原始 GeoIP 数据库。mmdb 将使用增强版 GeoIP2 数据库。
--enable-tcb=<memhash|btree>
Tokyo Cabinet 存储支持。 memhash 将使用 Tokyo Cabinet 的内存哈希数据库。 btree 将使用 Tokyo Cabinet 的磁盘 B+Tree 数据库。
--disable-zlib
禁止在 B+Tree 数据库上使用 zlib 压缩。
--disable-bzip
禁止在 B+Tree 数据库上使用 bzip2 压缩。
--with-getline
使用动态扩展行缓冲区用来解析完整的行请求,否则将使用固定大小(4096)的缓冲区。
--with-openssl
使 GoAccess 与其 WebSocket 服务器之间的通信能够支持 OpenSSL。
Syntax: log_format name [escape=default|json|none] string ...;
Default:
log_format combined "...";
Context: http
Specifies log format.
The escape parameter (1.11.8) allows setting json or default characters escaping in variables, by default, default escaping is used. The none value (1.13.10) disables escaping.
For default escaping, characters “"”, “”, and other characters with values less than 32 (0.7.0) or above 126 (1.1.6) are escaped as “xXX”. If the variable value is not found, a hyphen (“-”) will be logged.
For json escaping, all characters not allowed in JSON strings will be escaped: characters “"” and “” are escaped as “"” and “\”, characters with values less than 32 are escaped as “n”, “r”, “t”, “b”, “f”, or “u00XX”.
The log format can contain common variables, and variables that exist only at the time of a log write:
$bytes_sent
the number of bytes sent to a client
$connection
connection serial number
$connection_requests
the current number of requests made through a connection (1.1.18)
$msec
time in seconds with a milliseconds resolution at the time of the log write
$pipe
“p” if request was pipelined, “.” otherwise
$request_length
request length (including request line, header, and request body)
$request_time
request processing time in seconds with a milliseconds resolution; time elapsed between the first bytes were read from the client and the log write after the last bytes were sent to the client
$status
response status
$time_iso8601
local time in the ISO 8601 standard format
$time_local
local time in the Common Log Format
In the modern nginx versions variables $status (1.3.2, 1.2.2), $bytes_sent (1.3.8, 1.2.5), $connection (1.3.8, 1.2.5), $connection_requests (1.3.8, 1.2.5), $msec (1.3.9, 1.2.6), $request_time (1.3.9, 1.2.6), $pipe (1.3.12, 1.2.7), $request_length (1.3.12, 1.2.7), $time_iso8601 (1.3.12, 1.2.7), and $time_local (1.3.12, 1.2.7) are also available as common variables.
Header lines sent to a client have the prefix “sent_http_”, for example, $sent_http_content_range.
The configuration always includes the predefined “combined” format:
log_format combined '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';
优化,http 200 300 记录, 其余不记录
map $status $loggable {
~^[23] 0;
default 1;
}
access_log /path/to/access.log combined if=$loggable;
定时任务
0 0 1 * * goaccess -a -d -f /var/log/nginx/access.log -p /etc/goaccess.conf -o /var/log/nginx/goaccess.html 2> /var/log/nginx/goaccess.log