Using Cloudflare and showing visitors real addresses.

      Comments Off on Using Cloudflare and showing visitors real addresses.

Recently I did a few tests with CloudFlare. My goals were to see how it would affect performance on my site and what other noticeable effects it would have. One thing that I had to cope with is the fact that visitors to my site no longer had a unique IP address. Since CF worked as a CDN/Proxy in my case, it resulted in everybody having the same IP: CF’s. This is how I worked around that nuisance:

By default CF passes all HTTP headers as is from the client to origin. There are two exceptions, one of which we will use to determine the original IP from our visitors. This header is placed there by CF and holds each visitor’s IP address. This way we can see the different visitors again. All we need to do is extract that information and do something useful with it. The exact way this process works is shown at CF’s support environment. To make this work we can use the following code in our web.config:


What this does is look for a HTTP header CF_CONNECTING_IP, read its contents and set the REMOTE_ADDR value accordingly. This way your website can use the correct values for the visitor’s IP addresses. Enjoy!