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:
wget https://raw.github.com/Prajithp/mod_reverseproxy/master/mod_reverseproxy.c apxs -i -c -n mod_reverseproxy.so 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/mod_reverseproxy.so <IfModule reverseproxy_module> ReverseProxyEnable On #The header to use for the real IP address. ReverseProxyRemoteIPHeader X-Real-IP #What IPs to adjust requests for ReverseProxyRemoteIPTrusted 127.0.0.1 </IfModule>
That’s about it. After making this change, all future requests should be properly tracker to the correct IP address.