Monitoring and Parsing access.log file for Nginx on Linux

In this tutorial, we are going to see various command-line tools that can be used to parse access.log file for Nginx using awk. We will also see how we can monitor in real-time the access.log using ngxtop and finally, we will how to use GoAccess to quickly generate a visual server report of various statistics of access.log on the fly.

All the examples that are shown in this tutorial assume that your Nginx setup was done using WordOps.

Using awk for parsing access.log file

in the examples shown below ‘awxdocs.com.access.log‘ is the name of the access.log file for awxdocs.com website, while using it on your system replace it with the path to your access.log file. All these commands are run within /var/log/nginx directory.

Get entries within the last N hours using awk

In the example below we are showing entries to access.log file for the last 2 hours

Get entries within relative timespan using awk

Get entries within absolute timespan using awk and save it

In this example, we are extracting the entries from access.log and saving them to another file to make it easier to analyze by opening the file.

Get IP address using awk and sort number of requests per IP by count

In this example, we are listing the IP address and number of requests made by them

If you want to show only the top 10  IP addresses then you can use

Using awk search for the count of requests that missed the Cache

In this example, we will show the total count of URL requests that missed the cache

you can also list them using

Similarly, we can list all the URLs for which Cache was hit

Using awk to pattern match multiple columns and then list them

We will show how to match multiple columns and list those lines that are matching, in this example, we are showing the count of all the cache miss requests for the google bot

List all HTTP response status codes using awk

List top 10 404 URLs using awk

 

Using ngxtop to monitor access.log in real-time

we will see how to set up ngxtop on Ubuntu and then the most useful commands to monitor the Nginx access.log file for WordOPS.

Install PIP

Install ngxtop

Monitor access.log file in real-time with log format defined as per WordOPS Nginx setup

Parsing access.log file to show the summary

 

Using GoAccess to create reports by parsing access.log file

We will install the GoAccess on ubuntu and learn the commands to generate the reports in HTML format.

Installing GoAccess on Ubuntu

Updating the GoAccess configuration for WordOPS

you can find the exact path of goaccess.conf file being used using the following command

in our case, it was at /etc/goaccess.conf, so go ahead and edit this file,

and put the following lines at the end of the file, and save it.

in case your log format is different you will have to update the log-format using custom log format given on the GoAccess website.

Generating real-time reports from Nginx access.log

this command shows the real-time stats

Generating HTML report from access.log file

you can export the statistics shown on the terminal file as a self-contained HTML file using the command below.

This HTML file can then be opened in the browser for easier access.

 

Updated on Oct 19, 2021