Simple Server Side Analytics with Nixos, Nginx and GoAccess
I’ve been looking into a server-side solution for website analytics so I can reduce the amount of JavaScript needed to download on a website. I came across GoAccess, and it seemed to fit the bill. Setting it up on NixOS is undocumented, so I figured I’d drop a post on how I got it running.
What is GoAccess
GoAccess is a free CLI tool that helps you quickly see and understand website logs. Its output can be viewed within a terminal or web browser to get clear, real-time reports about your server’s activity.
Once it is installed, just point it at a log file and visualize what is happening on your web server:
goaccess access.log
Installing GoAccess on NixOS
The easiest way to get started is to open up an interactive Nix shell with the GoAccess package.
nix-shell -p goaccess
Now, in this shell, we can invoke the goaccess
commands.
Nginx and Finding Logs
Probably the hardest part of this process is finding the logs of your web server. In my case, I had to do some digging to find the default directory where Nginx dumps the logs. From digging on the Nix forums, I found that it logs to /var/spool/nginx/
. However, when I looked on my server machine, this directory did not exist. Sigh.
Luckily, it was not far off. A bit of Unix traversal wizardry, and I found that the logs were located at /var/log/nginx/
. Now that we’ve found our directory, we can start doing some analysis.
Running GoAccess
Putting the pieces together, getting it running in the terminal will simply look like:
goaccess /var/log/nginx/access.log
Notice that this is interactive. You need to select a log format for it to parse. We can pass in a flag to select one when running the command:
goaccess access.log --log-format=COMBINED
Web Analytics Replacement
Unfortunately, it’s not a complete replacement for client-side analytics. We’re restricted to the behaviors that are logged on our web server. This may be good enough for limited usage, but if you need more advanced tracking, you’ll need to fall back to a client-side solution.
Not a bad tool to have in the toolbox. Happy sys-admining!