GoAccess 是一款开源的实时 Web 日志分析器和交互式查看器,能够快速分析服务器日志并以直观的方式呈现结果。以下是关于如何使用 GoAccess 进行实时日志监控的详细介绍:
# Ubuntu/Debian
sudo apt-get install goaccess
# CentOS/RHEL
sudo yum install goaccess
# 从源码编译安装
wget https://tar.goaccess.io/goaccess-1.4.tar.gz
tar -xzvf goaccess-1.4.tar.gz
cd goaccess-1.4/
./configure --enable-utf8 --enable-geoip=legacy
make
sudo make install
brew install goaccess
goaccess /var/log/nginx/access.log -c
goaccess /var/log/nginx/access.log -o report.html --log-format=COMBINED --real-time-html
goaccess /var/log/nginx/access.log -o /var/www/html/report.html --log-format=COMBINED --real-time-html --daemonize
可以配置日志格式、忽略特定请求等:
time-format %H:%M:%S
date-format %d/%b/%Y
log-format %h %^[%d:%t %^] "%r" %s %b "%R" "%u"
--log-format=COMBINED
:指定日志格式--real-time-html
:启用实时 HTML 报告--daemonize
:以守护进程模式运行--port=7890
:指定实时 HTML 报告的端口--ws-url=wss://yourdomain.com
:指定 WebSocket URL--ignore-crawlers
:忽略爬虫请求--exclude-ip=192.168.0.1
:排除特定 IPgoaccess /var/log/nginx/access.log \
--log-format=COMBINED \
--real-time-html \
--port=7890 \
--output=/var/www/html/report.html \
--daemonize
然后可以通过浏览器访问 http://your-server-ip:7890/report.html
查看实时报告。
# 合并多个日志文件
zcat /var/log/nginx/access.log.*.gz | goaccess -a -o report.html
# 监控多个实时日志
tail -f /var/log/nginx/access.log /var/log/nginx/access.log.1 | goaccess -
可以将生成的 HTML 报告集成到现有 Web 服务器中:
使用 --debug
参数调试日志格式问题:
goaccess /var/log/nginx/access.log --debug
确保: 1. WebSocket 连接正常 2. GoAccess 进程正在运行 3. 防火墙允许指定端口通信
GoAccess 是一个功能强大且高效的日志分析工具,特别适合需要实时监控 Web 服务器访问情况的环境。通过合理配置,可以快速搭建一个实时的访问统计和分析平台。