Monitoring real time nginx traffic using GoAccess

Monitoring real time nginx traffic using GoAccess

Tracking Website visitors statistics is vital for every webmaster. one of the best way to do this is monitoring nginx logs. We have discussed about few popular open source nginx log monitoring tools in our previous blog. In this blog we are discussing how to monitor real time nginx traffic with GoAccess.

step-1:

Installing GoAccess

On installing goaccess via command line we get lower version hence it is recommended to download its tar file and install it via make.

installing dependencies for goaccess in Ubuntu/Debian
1. run $ sudo apt-get install libncursesw5-dev (For adding (NCurses dependencies)
2. run $ sudo apt-get install libgeoip-dev (optional GeoIp dependencies)
3. run $ sudo apt-get install libtokyocabinet-dev (optional Tokyo Cabinet dependencies)

Installation via tar file
run following commands to download,extract and compile GoAccess
$ wget http://tar.goaccess.io/goaccess-0.9.7.tar.gz
$ tar -xzvf goaccess-0.9.7.tar.gz
$ cd goaccess-0.9.7/
$ ./configure --enable--geoip --enable-utf8
$ make
$ make install

Enable time format, date format and log format for GoAccess
reach to $ cd /usr/local/etc/
use vim to view goaccess configuration file by using command $ sudo vim goaccess.conf
now uncomment (remove #) from these locations
1. Apache/nginx time format time-format %H:%M:%S
2. Apache/nginx date format date-format %d/%b/%Y
3. NCSA Comobined log format log-format %h %^[%d:%t %^] "%r" %s %b "%R" "%u"

Verifying GoAccess Setup
run this command to launch goaccess in terminal
$sudo goaccess -f /var/log/etc/access.log
now select your log format to generate goaccess log report.

GoAccess allows to generate real time nginx server statistics in CSV, JSON and HTML formats. Now we will generate GoAccess HTML report automatically using crontab and host it on our nginx server so that it can be access via a url. eg., www.yoursite.com/myvisitors.html

step-2:

Setting up nginx Server block

Create a new directory

sudo mkdir -p /var/www/site_name/public_html

adding -p with this command automatically generates all the parents of the new directory.

Grant Permissions

you need to grant permission to the right user, instead of root system. Replace "www-data" with appropriate user name.

sudo chown -R www-data:www-data /var/www/site_name/public_html

Make sure everyone having reading access to these files

sudo chmod 755 /var/www

Create Page

use the following command to generate GoAccess report in html format and tailor it to a suitable directory so it can be accessed using a url.

sudo goaccess -f /var/log/nginx/access.log -a > /var/www/site_name/public_html/report.html

create a new Virtual Hosts

we need to create a file that contains all the information of host file. By default nginx provide a layout of this file called default available at (/etc/nginx/sites-available/default/). We will copy this file and need to make some changes.

sudo cp /etc/nginx/site-available/default /etc/nginx/sites-available/site_name/

Configuring virtual host

sudo vim /etc/nginx/sites-available/site_name/

   server {
    listen 80 ;
  # listen [::]:80 default_server ipv6only=on; 
   root /var/www/site_name/public_html;
    report.html;

    # Make site accessible from http://localhost/
    server_name Enter your domain name or public ip here;

    location /reports {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules
    }
  • Uncomment "listen 80" so that all traffic coming through that port will be directed to that site.
  • change the root extension to match the directory, we have created
  • Change the server name to your domain name, or you can use your IP address.

Active host

Activate the host by creating a symbolic link between the site-available and site-enabled directory.

sudo ln -s /etc/nginx/sites-available/site_name /etc/nginx/sites-enabled/site_name

Remove default server block

for avoiding conflict delete the default nginx server block

sudo rm /etc/nginx/sites-enabled/default

Restart Nginx

sudo service nginx restart

step-3:

Setting Up crontab

(i) open crontab editor

run crontab -e

Select text editor most preferably nano or vim

(ii) Setup a Crontab for generating html page each and every hour

0 * * * * sudo goaccess -f /var/log/nginx/access.log -a > /var/www/site_name/public_html/report.html

Just save this code in crontab editor and exit

Here crontab will automatically run this command that will generate the log file and tailor it in the form of report.html.

Result/Conclusion

After successfully completing all these steps, you will be able to monitor real time nginx stats like unique visitors per day, Requested files (URLs), Static Requests, Visitors Host names and IP, 404 pages etc. in a neat html page.