GoAccess 是一款开源(MIT 许可证)的且具有交互视图界面的实时 Web 日志分析工具,通过你的 Web 浏览器或者 *nix 系统下的终端程序即可访问[1]。它为需要动态可视化报告的系统管理员提供了快速而有价值的 HTTP 统计信息[2]。GoAccess 解析指定的 Web 日志文件,并将数据输出到 X 终端。功能包括:
###################################### # Time Format Options (required) ###################################### # # The hour (24-hour clock) [00,23]; leading zeros are permitted but not required. # The minute [00,59]; leading zeros are permitted but not required. # The seconds [00,60]; leading zeros are permitted but not required. # See `man strftime` for more details # # The following time format works with any of the # Apache/NGINX's log formats below. # time-format %H:%M:%S
移除 time-format %H:%M:%S 前面的 # 即可。
2. 设置日期格式
1 2 3 4 5 6 7 8 9 10 11 12 13
###################################### # Date Format Options (required) ###################################### # # The date-format variable followed by a space, specifies # the log format date containing any combination of regular # characters and special format specifiers. They all begin with a # percentage (%) sign. See `man strftime` # # The following date format works with any of the # Apache/NGINX's log formats below. # date-format %d/%b/%Y
移除 date-format %d/%b/%Y 前面的 # 即可。
3. 设置日志格式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
###################################### # Log Format Options (required) ###################################### # # The log-format variable followed by a space or \t for # tab-delimited, specifies the log format string. # # NOTE: If the time/date is a timestamp in seconds or microseconds # %x must be used instead of %d & %t to represent the date & time.
# In addition to specifying the raw log/date/time formats, for # simplicity, any of the following predefined log format names can be # supplied to the log/date/time-format variables. GoAccess can also # handle one predefined name in one variable and another predefined # name in another variable. # log-format COMBINED
# The port to which the connection is being attempted to connect. # By default GoAccess' WebSocket server listens on port 7890 # See man page or http://gwsocket.io for details. # port 7890
# Write the PID to a file when used along the daemonize option. # #pid-file /var/run/goaccess.pid
# Enable real-time HTML output. # real-time-html true
# URL to which the WebSocket server responds. This is the URL supplied # to the WebSocket constructor on the client side. # # Optionally, it is possible to specify the WebSocket URI scheme, such as ws:// # or wss:// for unencrypted and encrypted connections. # e.g., ws-url wss://goaccess.io # # If GoAccess is running behind a proxy, you could set the client side # to connect to a different port by specifying the host followed by a # colon and the port. # e.g., ws-url goaccess.io:9999 # # By default, it will attempt to connect to localhost. If GoAccess is # running on a remote server, the host of the remote server should be # specified here. Also, make sure it is a valid host and NOT an http # address. # ws-url wss://example.com:443/wss/
使用加密的 WebSocket 连接更加安全,推荐使用 wss 协议。ws-url 需要根据 Web 服务器指定的 URL 来进行设置。
# Specify the path to the input log file. If set, it will take # priority over -f from the command line. # log-file /var/log/nginx/access.log
6. 日志解析器设置
(1) 排除解析的 IP 地址
在 exclude-ip 命令后面添加 IP 地址或者 IP 地址范围,可以让 GoAccess 不解析这些 IP 地址或 IP 地址范围的访问。推荐将本地 IP 地址都排除掉。
1 2 3 4 5 6 7 8 9 10
# Exclude an IPv4 or IPv6 from being counted. # Ranges can be included as well using a dash in between # the IPs (start-end). # exclude-ip 127.0.0.1 exclude-ip 192.168.0.0-192.168.255.255 exclude-ip 172.16.0.0-172.31.255.255 exclude-ip 10.0.0.0-10.255.255.255 exclude-ip ::1 #exclude-ip 0:0:0:0:0:ffff:808:804-0:0:0:0:0:ffff:808:808
(2) 解析输出文件设置
GoAccess 可以输出 csv、json 和 html 三种类型文件。通常设置为 html,其优点是能直接通过 Web 浏览器查看。
1 2 3 4 5 6 7 8
# Write output to stdout given one of the following files and the # corresponding extension for the output format: # # /path/file.csv - Comma-separated values (CSV) # /path/file.json - JSON (JavaScript Object Notation) # /path/file.html - HTML # output /var/www/html/goaccess/index.html
7. 解析 IP 地理位置
如果要启用 GoAccess 解析客户端 IP 地理位置的功能,我们还需要加载 GeoIP 数据库文件。GeoIP 数据库文件可以在 MaxMind 下载。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# === GeoIP2 # For GeoIP2 databases, you can use DB-IP Lite databases. # DB-IP is licensed under a Creative Commons Attribution 4.0 International License. # https://db-ip.com/db/lite.php
# Or you can download them from MaxMind # https://dev.maxmind.com/geoip/geoip2/geolite2/
# For GeoIP2 City database: # Download the GeoLite2-City.mmdb.gz # gunzip GeoLite2-City.mmdb.gz # # For GeoIP2 Country database: # Download the GeoLite2-Country.mmdb.gz # gunzip GeoLite2-Country.mmdb.gz # geoip-database /usr/share/GeoIP/GeoLite2-City.mmdb