For CloudFlare users: how to use CF-* fields from HTTP header instead of normal IP?

Since CloudFlare obfuscates the visitor’s IP address, it would be useful to be able to capture additional CloudFlare specific fields such as CF-Connecting-IP, X-Forwarded-For, and CF-IPCountry from the HTTP header that they insert and then conditionally report these within the Admin > Users > username page in addition to the (not-useful with CF) IP fields + IP Lookup button.

Could we please have these field provided within this page when CF-* header fields are available?

One option that would be useful (and I’ve used in other Rails apps) is to just use those fields instead of the IP if they’re available. It’s been a while since I implemented that, but if I recall correctly I believe we would just set visitor’s IP to IP unless CF-Connecting-IP exists… but I thought there was something else I had done with X-Forwarded-For

1 Like

If you use the provided Cloudflare template this is all handled automatically. This has actually been available within Discourse for four or five years.

Assuming you followed the standard install:

cd /var/discourse
nano containers/app.yml

then add this line under templates:

- "templates/cloudflare.template.yml"

Then just rebuild:

./launcher rebuild app

At which point your install will show true user addresses.

5 Likes

That’s fantastic Stephen, thank you! Such a simple thing that it should probably be in there and commented out by default or documented somewhere. At least one can find it via “CloudFlare” searches here now in relation to this. Never even crossed my mind to look into the templates folder! :slight_smile:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.