Skip to content

Instantly share code, notes, and snippets.

@linuxmalaysia
Last active March 23, 2024 22:10
Show Gist options
  • Save linuxmalaysia/c6e2451cbe0f3ef2f16b04608deda615 to your computer and use it in GitHub Desktop.
Save linuxmalaysia/c6e2451cbe0f3ef2f16b04608deda615 to your computer and use it in GitHub Desktop.
Terminal-Based Log Analysis with GoAccess for Nginx

Unleash the Power of Terminal-Based Log Analysis with GoAccess for Nginx

Running a website necessitates constant monitoring. However, wading through complex web interfaces to analyse server logs can be time-consuming. Enter GoAccess, a free, open-source tool that empowers you to analyse your Nginx server logs directly from the terminal.

Why GoAccess?

  • Blazing Speed: Gain real-time insights instantly, ditching the wait for reports.
  • Terminal Magic: Analyse logs directly from the command line, perfect for SSH access or terminal enthusiasts.
  • Flexible Output: Go beyond the terminal with reports in HTML (ideal for sharing and visualisation), JSON, and CSV formats.

Getting Started with GoAccess and Nginx

Installation is straightforward: Use your package manager (e.g., apt install goaccess on Debian-based systems). Refer to the GoAccess documentation (https://goaccess.io/man) for specific details.

Instant Analysis of Your Current Access Log

Need a quick snapshot of your website's current traffic? The following command analyses your Nginx access log (/var/log/nginx/access.log) in real-time using the standard Nginx log format (COMBINED):

tail -f /var/log/nginx/access.log | goaccess -r /var/log/nginx/access.log -f -

Explanation of the Real-Time Command:

  • tail -f /var/log/nginx/access.log: This part uses tail -f to continuously monitor the log file for new entries as they're added.
  • goaccess: Invokes the GoAccess program.
  • -r /var/log/nginx/access.log: This flag specifies the log file to be analysed. Note that it's the same file being monitored by tail -f.
  • -f: Instructs GoAccess to run in live mode, processing data as it arrives from tail.
  • -: This tells GoAccess to read data from standard input (the piped output from tail -f).

If your logs are rotated at midnight (based on your logrotate settings), you can use this approach for continuous monitoring of new entries:

goaccess --log-format=COMBINED /var/log/nginx/access.log

The -c flag's behaviour may vary depending on your GoAccess version, so consult the documentation for specifics.

This command will instantly analyse your access log and present the results within your terminal. Remember, for a more comprehensive picture, it's recommended to let GoAccess collect data for 15-30 minutes in real-time scenarios.

Real-Time Analysis for Logs with Midnight Rotation (Daily or Weekly):

Beyond the Basics

The GoAccess documentation offers comprehensive information on advanced configuration, supported log formats, and various output options: https://goaccess.io/man

Join the Community!

Engage with the GoAccess community for discussions, tips, and tricks. Many online communities cater to web server administration, including the LinuxMalaysia Community: https://t.me/linuxmalaysiamy/138

@linuxmalaysia
Copy link
Author

Note formated and checked by Google Gemini.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment