Easiest Way to Log Users IP with CloudFlare and Nginx
If you use a proxy such as CloudFlare you might have noticed that the remote IP address in your access logs will not be the users, but instead CloudFlare’s. This is since CloudFlare forwards all requests to your server. When building Klart.co I wanted to get the users addresses in the logs, and I’ll explain how I did it here.
It turns out there are several ways, that gives you various functionality. The simplest way I found to just display the users IP in the access log was to add this line to the nginx.conf http block:
log_format main ‘$http_x_forwarded_for — $remote_user [$time_local]‘
‘”$request” $status $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent”’;
And the following line to your server block
access_log /your/access/log/path main;
This will log the $http_x_forwarded_for as the IP addresses instead of CloudFlare’s.