Overriding which IP address the geolocation features use

  Last updated March 29, 2017

By default the geolocation lookup is based on the IP address of the user. In some cases, such as with traffic through proxies, this type of lookup doesn't work properly. In particular, users of Opera Mini always browse through a proxy and the true IP address appears in the X-Forwarded-For header. Similarly, the Amazon Silk browser can optionally come through a proxy, indicated via the User Agent string.

To work around this and account for both the Opera Mini and Amazon Silk browsers, you would use code like this in vcl_recv:

if ((req.http.X-OperaMini-Features || req.http.User-Agent ~ " Silk-Accelerated=true$") && req.http.X-Forwarded-For ){
  set client.geo.ip_override = req.http.X-Forwarded-For;

which tells Fastly to use the X-Forwarded-For header value for the IP address. If it is not available, then the code will fall back to using the IP address of the client.

Finally, just in case there's some scenario or browser we haven't anticipated, you can also override based on an arbitrary header:

set client.geo.ip_override = req.http.Custom-IP-Override;

Setting this variable will force the geolocation information to be reloaded.

Back to Top