Getting real IP addresses using CloudFlare and Nginx
Before you start
Before you start, you have to check if your nginx have been compiled with --with-http_realip_module, you do that by running the command nginx -V and look for the module.
This is my output from an standard Debian installed nginx package.
Nginx configuration
The documentation for HttpRealipModule module states that the configuration should be in either http, server or location context. You probably want to add these server wide, in /etc/nginx/nginx.conf. But this file is likely to be overwritten by an update, so I recommend to make a cloudflare.conffile in /etc/nginx/conf.d.
Content of /etc/nginx/conf.d/cloudflare.conf
CloudFlare do change their IPs from time to time (they keep an updated list online), and since we have the list of IPs in an separate file, its easy to automate an update of these.