Obtaining the real IP for AWStats when using Varnish
Ever since i installed Varnish Cache on the server to speed up website delivery and overall performance i had no visitors showing up in AWStats except for one, the IP of the Varnish server. From the technical side of things that is perfectly fine, since the only IP/Visitor accessing Apache is Varnish which runs locally. But i wanted to have visitors show up in AWStats again, i kinda like to see how many unique visitors, pageviews, etc. the site receives without having to resort to services like Google Analytics.
Well, this has been bugging me for a while now and today i took the time to look a little closer into getting it sorted and setup everything in a way that AWStats can log the real IPs of visitors again and display its statistics. There are different approaches, one of them would be reinstalling AWStats from scratch, messing around with the log file formats and some other, highly complicated stuff. Since AWStats was working fine before the installation of Varnish i felt no need to reinstall it.
So here is how i fixed it without having to reinstall AWStats and mess with the Apache log file format. Since i am using cPanel and Apache i decided to use the mod_rpaf module. mod_rpaf doesn’t come with a standard cPanel/WHM installation, so it need to be installed first before you can use it. To download and install it run the commands below in your command line
tar -xzf mod_rpaf-0.6.tar.gz
apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c
When done you need to load the mod_rpaf module into your Apache configuration. To do so login to your WHM interface, open Service Configuration > Apache Configuration > Include Editor > Pre Main Include and select All Versions from the dropdown list, then paste the text below the screenshot into the editor:
Here is the text to paste:
LoadModule rpaf_module modules/mod_rpaf-2.0.so
RPAFproxy_ips 127.0.0.1 18.104.22.168
When pasting, make sure you edit the IP address (22.214.171.124 in the above example) to match the IP of your server. When done pasting and changing the IP click Update and then restart Apache to apply the changes. Now there is just one step left to do before you have your AWStats functionality back when running cPanel/WHM and Varnish. To make cPanel remember that Apache configuration template has changed we need to run the following commands:
/usr/local/cpanel/bin/apache_conf_distiller --update --main
And that’s it. You can now quit your terminal/shell session and AWStats should log visitors just fine.