我想对进入我的服务器的流量进行一些分析。一般来说,自助托管者中最受欢迎的选择是Plausible和Umami 。通过这些类型的服务,您可以获得添加到网站的跟踪脚本。所以我的想法是使用 Traefik(我选择的反向代理)将脚本注入到我托管的每个应用程序的 HTML 中。但在找不到简单的方法后,我找到了 GoAccess 的替代解决方案。这是一个可以从 Apache、Nginx、Caddy 和 Traefik 等多个来源读取日志并生成简单报告的工具。
设置 Traefik
首先,您需要 Traefik 将访问日志写入文件,因为默认情况下它会写入 stdout。为此,您需要将以下内容添加到traefik.yml
中:
accessLog : filePath : " /path/to/access.log"
或者,如果您使用的是traefik.toml
:
[accessLog] filePath = "/path/to/access.log"
如果您通过 Cloudflare 使用代理流量,您将看到日志中的 IP 地址来自 Cloudflare。要获取真实的 IP 地址,您需要将以下内容添加到traefik.yml
中:
entryPoints : web : address : " :443" forwardedHeaders : trustedIPs : - 173.245.48.0/20 - 103.21.244.0/22 - 103.22.200.0/22 - 103.31.4.0/22 - 141.101.64.0/18 - 108.162.192.0/18 - 190.93.240.0/20 - 188.114.96.0/20 - 197.234.240.0/22 - 198.41.128.0/17 - 162.158.0.0/15 - 104.16.0.0/13 - 104.24.0.0/14 - 172.64.0.0/13 - 131.0.72.0/22
如果您使用traefik.toml
:
[entryPoints] [entryPoints.web] address = ":80" [entryPoints.web.forwardedHeaders] trustedIPs = [ "173.245.48.0/20" , "103.21.244.0/22" , "103.22.200.0/22" , "103.31.4.0/22" , "141.101.64.0/18" , "108.162.192.0/18" , "190.93.240.0/20" , "188.114.96.0/20" , "197.234.240.0/22" , "198.41.128.0/17" , "162.158.0.0/15" , "104.16.0.0/13" , "104.24.0.0/14" , "172.64.0.0/13" , "131.0.72.0/22" ]
使用GoAccess
您可以在此处查看如何安装 GoAccess。对于 Debian/Ubuntu,您可以使用以下命令安装它:
apt-get install goaccess
要获取终端上的统计信息,您可以运行:
goaccess access.log -c
这将使您可以选择一种预定义的日志格式,对于 Traefik,您将需要选择Common Log Format (CLF)
。
在终端中,您可以使用TAB
滚动浏览不同的统计数据,或使用0-9
和Shift + 0
选择所需的统计数据。您还可以使用j
和k
在每个统计数据中上下滚动。
或者,如果您希望将统计信息发送到文件(例如 HTML 文件),您可以运行:
goaccess access.log --log-format=COMBINED -o report.html
然后,您可以使用 Web 服务器提供 HTML 文件,或者通过使用scp
之类的内容将其复制到本地计算机来在浏览器中打开它:
scp node:~/docker/traefik/report.html ~/Downloads/report.html
它还可以选择在实时 HTML 报告中显示统计信息。为此,您可以运行:
goaccess access.log --log-format=COMBINED -o report.html --real-time-html
或者,如果您更喜欢 Docker,则可以通过运行以下命令将日志从外部环境传输到 Docker 进程:
cat access.log | docker run --rm -i -e LANG=$LANG allinurl/goaccess -a -o html --log-format COMBINED - > report.html