Skip to content

IP geolocation

The VIP edge cache respects the vary: X-Country-Code response header, making it possible to deliver variations of cached content based on an end user’s global location. Logic at the edge determines which specific cached version will be delivered in response to a user’s request based on the country code value in the incoming X-Country-Code request header.

Limitations

  • Vary responses should be limited to a specific set of page URLs that absolutely require it. Adding a Vary response for every page can negatively affect the page cache hit ratio and overall performance of a site. For Node.js applications built on Next.js, the source property can be utilized to match specific paths.
  • Geolocation on the VIP Platform is not reliable for sites behind a reverse proxy. Requests that pass through a reverse proxy for a site on VIP will have the remote IP address of the reverse proxy instead of the requester’s IP address. When using a reverse proxy, it is possible that the correct country might be served to the end user, but in most cases, it will be incorrect.

Geolocation for Node.js

Node.js applications can read the country code value of a request in the X-Country-Code request header. The recognized set of country codes is powered by the Maxmind GeoIP database and kept current by VIP.

The exact methods required to read the value of the X-Country-Code request header and set response headers to vary the cache is specific to an application’s Node.js framework. To identify the correct method, search the framework’s documentation for terms similar to “setting response headers”.

Example code in the vip-go-node-skeleton repository demonstrates passing a request object to the server as req. In that use case, the country code value of a request can be be retrieved with req.headers['x-country-code'].

Geolocation in PHP

WordPress applications on the VIP Platform can leverage the $_SERVER['GEOIP_COUNTRY_CODE'] superglobal for geolocation. This superglobal is set to an ISO-3166 country code (ISO-3166-1 alpha-2) based on a site visitor’s IP address. The recognized set of country codes is powered by the Maxmind GeoIP database and kept current by VIP.

Updating a post or page in the WordPress site will invalidate the cached versions of that URL for all country codes.

VIP Go Geo Uniques plugin

The VIP Go Geo Uniques plugin is specifically designed for WordPress sites on the VIP Platform and leverages the $_SERVER['GEOIP_COUNTRY_CODE'] superglobal described above. A default country code and a list of supported country codes must be configured for the plugin to efficiently deliver different versions of content from the page cache to users based on location. More detailed instructions for installation and usage can be found in the plugin’s GitHub repository.

Last updated: July 31, 2024

Relevant to

  • Node.js
  • WordPress