Fixing your statistics software when using nginx as a reverse proxy

November 16, 2015

Whenever you use Nginx as a Reverse Proxy on your server you are likely to have issues with your server analytics being confused about where traffic is coming from. This is because the requests would appear to be coming through the main server IP and not the actual client requesting the files. This is for the most part correct behavior under that context but that’s probably not what you want or where expecting. Fortunately there is a simple fix for it.

What you need is an apache module called: mod_remoteip. Once installed and configured it will filter out your server IP address and use the remote client request IP instead as the requesting IP which will fix your analytics software. Setting up mod_remoteip is relatively simple also, just follow the steps bellow:

apxs -i -c -n mod_reverseproxy.c

These are two separate commands, the first one downloads the module while the second one adds it to your working apache as an extension.

Once installed, all that is left is that you load the module and configure it. To that end, simply open up your httpd.conf file (or use the Include Files editor in cPanel / WHM if this is a cPanel server) and copy / paste the lines bellow AFTER making the right adjustments to your environment. These adjustments would be the path to the .so file (which you should have available from the verbose output of the apxs command) and the IP addresses being used on your server which need filtering:

LoadModule reverseproxy_module modules/

<IfModule reverseproxy_module>
ReverseProxyEnable  On
#The header to use for the real IP address.
ReverseProxyRemoteIPHeader X-Real-IP
#What IPs to adjust requests for

That’s about it. After making this change, all future requests should be properly tracker to the correct IP address.