General configuration
Using awstats to analyse your web usage statistics if your Apache webserver is running behind an Nginx reverse proxy server, needs some special configuration steps.
Install awstats on your system:
sudo apt install awstats
Enable the awstats apache configuration (and cgi if it is not enabled yet):
sudo a2enmod cgi
sudo a2enconf awstats
Restart the apache service:
sudo systemctl restart apache2
Edit the configuration awstats configuration file or create a configuration file for each virtual host. We are using combined log formats and need to configure this.
sudo cp /etc/awstats/awstats.conf /etc/awstats/awstats.www.mysite.com.conf
sudo nano /etc/awstats/awstats.www.mysite.com.conf
Configure the virtual host log file path in your awstats configuration file. Make sure you select the correct one (might be the SSL log file).
LogFile="/var/log/apache2/www.mysite.com.access_log"
LogFormat=1
Configure the domain name and domain aliases:
SiteDomain="www.mysite.com"
HostAliases="mysite.com XYZ.mysite.com"
Optionally, if you want DNS lookup (conversing of IP’s into hostnames):
DNSLookup=1
Now update the awstats statistics folder by running the following command:
sudo /usr/lib/cgi-bin/awstats.pl -config=www.mysite.com -update
Go to your awstats page:
https://www.mysite.com/cgi-bin/awstats.pl?config=www.mysite.com
Depending on your configuration, cgi-bin might need to be replaced by awstats.
Note on security: Make sure that your awstats statistics page is not visible from outside, even more if you are allowing your statistics to be updated through the web interface. I am forcing an htaccess realm through the Nginx reverse proxy on these directory.
If you want to update your statistics through the web interface, set the corresponding flag.
AllowToUpdateStatsFromBrowser=1
The following files have to be readable or writable by the apache service (www-data) user:
/var/lib/awstats (read & write)
/var/log/apache2/www.mysite.com.access_log (read)
Alternatively, add a crontab (edit /etc/crontab) entry:
* */15 * * * root /usr/lib/cgi-bin/awstats.pl -config=www.mysite.com -update > /dev/null
Configuration when running your web server behind an NGINX reverse proxy
Configure your reverse proxy correctly
In the NGINX reverse proxy configuration for your website, make sure you send the real IP-address of the client requesting your page through the header information to the web server. Add the following lines to the root location of your NGINX reverse proxy configuration for your web site (sudo nano /etc/nginx/sites-available/www.mysite.com). For more information on this, check here.
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
Configure your web server to write the correct IP of the client into the log file (instead of the reverse proxy IP). Add the following to your apache virtual host file (sudo nano /etc/apache2/sites-available/www.mysite.com.conf):
RemoteIPHeader X-Real-IP
RemoteIPInternalProxy IP.PROXY.SERVER
RemoteIPTrustedProxy IP.PROXY.SERVER
RemoteIPHeader X-Forwarded-For
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
%{X-Forwarded-For}i replaces the first parameter in the combined log format, which normally is %h or %a. So if the LogFormat is set in your configuration file, make sure it has the above format.
Apache needs the remoteip module to be enabled in order to treat this information:
sudo a2enmod remoteip
To load the following module in the main Apache configuration file (sudo nano /etc/apache2/apache2.conf), delete the hash sign (#) before the following line:
LoadModule remoteip_module modules/mod_remoteip.so
Test the configuration and reload the apache service.
apache2ctl configtest
service apache2 restart
If you believe you have header issues and don’t know if the bug is on the level of the reverse proxy or the web server, check what header information is arriving at the web server.