Page rule to add geo coordinates to header

Hello, does Cloudflare have a service (like a page rule) that will automatically add a header of a client’s estimated location (not just country) based on their IP address? Like the client’s US city, state, and geo coordinates (latitude and longitude).

I’m currently using Cloudflare plus a third party IP lookup service to get a browser-client’s location based on their IP address to deliver regional (e.g. Los Angeles or NYC) content for a marketplace. But this requires a couple of round-trip requests to different servers in order to deliver the regional content. It would be nice if my server was able to determine the region from the header and then server static content for that region.

Does Cloudflare or anybody have a solution to this?

RS

That would a Transform Rule for the Request Header:

Available ip.src.* and ip.geoip.* fields are listed here:

2 Likes

there’s a pre-built rule you can turn on with one click

It’s under the “Managed Transforms” button

5 Likes

(post deleted by author)

Hello and thanks for the quick response!

Is it possible to use the latitude (ip.src.lat) and longitude (ip.src.lon) in the request header (from the Cloudflare Managed Transform) to add a cookie in the HTTP response?

Thanks for the quick response! New question:

Is it possible to use the latitude (ip.src.lat) and longitude (ip.src.lon) in the request header (from the Cloudflare Managed Transform) to add a cookie in the HTTP response?

Probably…

First of all, what the managed rule does is take certain bits of data that are already available in variables (named ip.src.lat, ip.src.lon and such) and make that data visible to your origin server by putting them in various request headers

Even without the managed rule, those variables are still available to all transform rules

For example, here’s a very basic response header modification rule that might get you started:

This results in a response header that looks like this:

cookie: 24.00000

That’s not a properly-formed cookie obviously. You’ll need to build on it. You can put more stuff in the “Value” field, presumably including text strings such that you can construct a proper cookie. But you’re going to have to read the documentation because it’s kind of stubborn and I don’t really feel like messing with it more right now.

This is the main document for Transform Rules but please note that it links to many sub pages:

You’ll also likely need this which is not part of the Transform Rules documentation but the link to it is buried in there somewhere:

that should hopefully help you mangle the “Value” field into something usable

2 Likes

actually I messed with it a little more, what do you think about this?

that gives a response header like this:

cookie: latitude=24.00000; longitude=54.00000

I’m testing from an overseas VPS for privacy, usually the latitude and longitude aren’t just “.00000”, connecting from a hosting platform probably confuses the geolocation.

I’m curious why you actually need the cookie, though. Instead of having your origin server read the data from the cookie, why not just use the managed rule have your origin server read the data from the request headers?

2 Likes

Gotcha, thank you! This was super helpful and I appreciate the quick response. :slight_smile:

Just in case it wasn’t clear, these are how the headers will actually be named from the perspective of the origin server:

cf-ipcity
cf-ipcountry
cf-ipcontinent
cf-iplongitude
cf-iplatitude

also if you see “T1” for the continent that’s a Tor connection

2 Likes

I was hoping it would be possible to create a function within the transform rule using the IP lat/long as arguments. Something like “if the IP lat/lng is close to region A, then return “regionA”, if close to regionB, then return “regionB”, and so on”. I realize now this is beyond the scope of the transforms.

Anyway, I’m using Firebase Hosting (static CDN) and they have an i18n feature now that uses the user’s country to deliver a localized static page. You can override the Firebase country selection with a cookie “firebase-country-override={countryCode}”. I actually want to deliver content based on a region like Los Angeles or New York City instead of a country. So if I could use Cloudflare to calculate this region from the IP lat/lng and then set it in a cookie “firebase-country-override=los-angeles”, then I could deliver my localized static content for Los Angeles or New York City.

Anyway, I’m blocked on two counts:

  1. I can’t figure out how to set cookies in the response header. Is it possible?
  2. I can’t add a function to calculate the region for the cookie based on the IP lat/lng.

Unless there’s another way in Cloudflare, it looks like I’d have to use a server to do this.

Oh interesting! I didn’t know about the concat() function. I’ll try that. Thanks!

Would these variables possibly be useful to you?

Unfortunately it looks like they’re only available to paid plans so I can’t actually try them myself

concat("region=" , ip.geoip.subdivision_1_iso_code) or concat("region=" , ip.geoip.subdivision_2_iso_code) in the value field should do something, IF you have access to it (I don’t)

  • Access to ip.geoip.is_in_european_union, ip.geoip.subdivision_1_iso_code, and ip.geoip.subdivision_2_iso_code fields requires a Cloudflare Business or Enterprise plan.

possibly a better option

1 Like

Thanks for flagging. I actually just need the IP lat/lng. Unfortunately I’d have to account for every ip.geoip.subdivision_1_iso_code , and ip.geoip.subdivision_2_iso_code to determine my four regions and that seems kind of messy/failure prone.

I’m on the free plan too, and would upgrade if I could define my own function to calculate my region. But that doesn’t seem possible.

Thanks again for your help. Much appreciated.

Looks like Cloudflare Workers is the solution I was looking for.