AWStats is a log analyzer and supports Apache well. To use it with Nginx, however, you should fine-tune some details.

But for now, let’s get familiar with AWStats first. This package should be found in most Linux distributions. After installing the package, you should have a look at the following files, which may be scattered across your system, depending on the packager… But you should be able to find them using dpkg -L awstats or pacman -Ql awstats, etc.

  • awstats.pl - The main program which does the most work.
  • awstats_configure.pl - Generate a configuration file interactively.
  • awstats_updateall.pl - Update log database for each configuration specified in a certain directory.
  • awstats_buildstaticpages.pl - Generate static html pages for a configuration.

BTW, the database directory is /var/lib/awstats.

Great, now let’s start to configure AWStats. Below are the jobs we need to do:

  • Cut log files on a daily basis.

Nginx isn’t born with this feature (at least for version below 0.7.67). So you have to cut the log file each day by yourself. Cut is nothing but moving the log file to a new place and telling Nginx to generate a new one:

    mv /var/log/nginx/access.log /var/log/nginx/access_`date +%Y%m%d`.log
killall -s USR1 nginx

USR1 is a signal such that, when passed to nginx, will cause nginx to reopen the log file.

Save the code above as cut.sh and add it to crontab, setting the time to 23:55 on each day.

  • Generate a configuration for your site.

Run the command

    perl awstats_configure.pl

Then answer each question interactively. A new configuration file like awstats.www.sitename.com.conf should be generated in /etc/awstats.

Note that awstats_configure.pl needs a awstats.model.conf file. In case that your packager doesn’t place it in the right place, you should either place it in the right place, or modify the settings in awstats_configure.pl.

Next, open the generated configuration file. Change the LogFile option to something like:

    LogFile = "/var/log/nginx/access_%YYYY-0%MM-0%DD-0.log"

This will cause AWStats to look for the log of the current day. Also change the LogFormat to

    LogFormat = "%host - %host_r %time1 %methodurl %code %bytesd %refererquot %uaquot %otherquot"

This defines the log format that AWStats will recognize. And we should make Nginx actually write logs in this format, by setting in nginx.conf (or site- specific configuration files):

    log_format awstats
'$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "http_x_forwarded_for"';

access_log /var/log/nginx/access.log awstats;

Now AWStats should be able to recognize the log files of Nginx. (You may need to restart Nginx, of course.)

  • Generate static html pages.

For simplicity, we jump off the usage of awstats.pl (although it does the real job) and directly use awstats_buildstaticpages.pl. This program will first update the database (if specified with -update option, wich we do), then write results to static html pages. The command is as follows:

    perl awstats_buildstaticpages.pl -config=www.sitename.com -update -dir=/output_dir

This should generate about 20 html pages in output_dir. The main page is awstats.www.sitename.com.html. Just view it with your browser.

You should also save the code above as buildpages.sh and add it to crontab, setting the time to 23:56 or 23:57. This will make sure logs are first cut then html pages are built at the end of each day.

  • Protect your statistic data with authorization.

You may not want others to see these pages. So you may want to add the following lines in the proper position (http, server or location) of nginx.conf

    auth_basic "admin";
auth_basic_user_file .htpasswd;

The .htpasswd is generated using

    htpasswd -c .htpasswd admin

This article provides very useful information:

http://www.ibm.com/developerworks/cn/linux/l-cn-awstats-nginx/index.html