Configuring Awstats for Nginx
In this post I want to go over an example of how to get Awstats working correctly with Nginx server log files.
All my notes are based on Linux Ubuntu. If you’re using CentOS, RedHat or OpenSuse, you won’t run into any fundamental differences, aside from package names and config file locations.
So, first we need to install Awstats and configure nginx to work with perl.
apt-get install awstats
Next you need to add these lines to the Nginx virtual host config file.
location /awstats/ {
root /usr/lib/cgi-bin;
index index.html index.htm index.pl;
}
location /awstatsclasses/ {
alias /usr/share/awstats/lib/;
}
location /awstats-icon/ {
alias /usr/share/awstats/icon/;
}
location /awstatscss {
alias /usr/share/doc/awstats/examples/css/;
}
Next, let’s point awstats at the nginx logs. Let’s move on to configuring the parser:
cp /etc/awstats/awstats.conf /etc/awstats/awstats.my_cool_site.com.conf
If you have more than one site on the same server, it’s better to create separate config files for them. Next let’s edit the newly created file:
/etc/awstats/awstats.my_cool_site.com.conf
Open it in your favorite editor, then find and edit the following values according to your server’s config:
LogFile="/var/log/nginx/site_access.log" #path to logfile;
LogFormat=1 #for full statistics;
SiteDomain="my_cool_site.com" #domainname;
HostAliases="www.my_cool_site.com my_cool_site.org" #site aliases
The next step is creating a crontab job for regular log file parsing. Naturally it’s good practice to run it with superuser (root) rights:
*/20 * * * * /usr/lib/cgi-bin/awstats.pl -config=my_cool_site.com -update > /dev/null
Now the log file with site access info will be parsed every 20 minutes. You can increase the interval, but then parsing will need more time and resources. Awstats has its own metadata storage where it saves info about processed logs. That means on every run the parser continues from where it left off last time.
To get the statistics via web, the link will look like this: http://my_cool_site.com/awstats/awstats.pl?config=my_cool_site.com
I strongly recommend protecting the stats page with some kind of login form. For this, let’s go back to the Nginx config file where we added the lines needed for Awstats to work, and bring the location /awstats/ section to the following form:
location /awstats/ {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/auth/htpasswd;
root /usr/lib/cgi-bin;
index index.html index.htm index.pl;
}
Create the /etc/nginx/auth/ folder and generate the htpasswd file:
mkdir /etc/nginx/auth/
htpasswd -cmb /etc/nginx/auth/htpasswd username password
Just in case, let’s check the Nginx config and restart it.
Go to the address and check out the stats: http://my_cool_site.com/awstats/awstats.pl?config=my_cool_site.com
Please keep in mind that the log format your Nginx writes in might differ from mine. If the stats end up empty, you’ll have to go back to editing the file:
/etc/awstats/awstats.**my_cool_site.com**.conf
And play around with the LogType value, or adjust your Nginx log format to match the format Apache writes logs in.