Overriding which IP address the geolocation features use

      Last updated August 01, 2018

    By default 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. In cases like these, the X-Forwarded-For header will contain a comma-separated list of IPs instead of just one IP. Attempting a geolocation lookup on anything other than a single IP will result in a lookup failure, so you need to ensure the lookup is done on the end-user's IP only.

    To work around this and to 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 = regsub(req.http.X-Forwarded-For, ",.+$", "");

    which tells Fastly to use only the first IP in the X-Forwarded-For header as the 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

    Additional resources: