Configure Nginx to format logs and set up a server block.
Open the Nginx configuration file:
Add the following log format into your http group in nginx:
Warning: This logs the users IP address directly. It's not recommended to do it in this fashion, if possible anonymize the address as seen below.
(optional) Instead anonymize IP addresses in logs:
Configure a server block:
Test the new configuration:
Reload Nginx to apply changes:
Log rotation in Nginx is a process for managing log files to prevent them from becoming excessively large and consuming too much disk space. As Nginx continuously logs web requests, these files can grow rapidly. Without rotation, they can lead to performance issues and make log analysis more difficult. The default setting is for log rotation is daily, which means that the logs that goaccess can use for its reporting are also only daily. To increase that timeframe, do the following:
Edit log rotation configuration:
Add the configuration, please change the monthly to daily or weekly if you need daily or weekly rotation of the logs.
Apply the new rotation configuration:
Generate a HTML report:
If you wish to automate this, use crontab to generate recurring reports:
Open crontab for editing (use sudo, otherwise crontab will not access to the log file):
Add the line to automate hourly report generation:
Goaccess is a powerful tool when it comes to providing usage statistics for your endpoints.
This tutorial will guide you through configuring Nginx for logging, anonymizing logs, monitoring web traffic with GoAccess, and setting up log rotation for Nginx logs.
This guide is intended for intermediate users who are familiar with Linux, Nginx, and using the command-line interface.
Before configuring Nginx, install GoAccess, a real-time web log analyzer.
Update your package lists:
Install GoAccess: