I’m not really a fan of analytics and tracking people, but I recently found goaccess and found it pretty interesting. It’s a very simple tool that analyzes logs of your webserver to create statistics from them. So I gave it a quick try and it’s actually very simple to setup and you can even make it create live statistics with support for serving updates via websockets.
The setup process is fairly simple:
- Install goaccess via package manager or compile from source (it’s written in C, not go btw)
- Make sure nginx writes logs
- Create a systemd service that manages goaccess
- Configure nginx to serve the statistics and reverse proxy the websocket
Getting nginx to write logs is pretty straightforward:
server {
[...]
error_log /var/www/example.com/logs/error.log;
access_log /var/www/example.com/logs/access.log;
[...]
}
The systemd service is also pretty simple:
[Unit]
Description=Realtime statistics for example.com
After=syslog.target
After=network.target
[Service]
Type=simple
# set user and group
User=root
Group=root
# configure location
WorkingDirectory=/root
ExecStart=goaccess /var/www/example.com/logs/access.log -o /var/www/example.com/html/stats/main.html --real-time-html --ws-url=example.com/stats/ws --addr=127.0.0.1
Restart=always
RestartSec=15
[Install]
WantedBy=multi-user.target
Finally we’ll just have to reverse proxy the websocket which we’ve configured to
bind to 127.0.0.1
to prevent people from directly connecting to it:
server {
index index.html;
root /var/www/example.com/html;
location /stats/ws {
proxy_pass http://127.0.0.1:7890/;
proxy_set_header Host $host;
# Set headers for proxying WebSocket
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect http:// $scheme://;
}
}
7890
is the default port. If you want to generate statistics for multiple sites
you’ll have to use different ports for each site. That’s basically it, you now
have realtime statistics without having to adding any stupid Javascript to your
website. Obviously you also won’t get any more info than the logs provide,
but you get a nice overview.