KeyCDN Launches Country Based Caching

By Sven Baumgartner
Published on March 3, 2022
KeyCDN Launches Country Based Caching

KeyCDN supports now country code based caching of your assets. Our edge servers derive the two-letter country code from the client IP address, that code is used for the cache key and forwarded to the origin server. Each time the client comes from a different country creates a dedicated cache entry.

Why is country based caching useful?

This is especially useful if your website should provide different content for countries, such as legal disclaimers. KeyCDN country based caching allows you to deliver different versions of your content that comply with legal requirements, depending on which country your website visitor is located in.

Configure caching depending on the viewer's location

You can enable the Cache Key Country option in your Zone settings. The country code is derived from the client IP address. This will add the X-Country-Code header to the origin request with the two-letter country code (ISO 3166-1 alpha-2).

Here is an example of what a request would look like. Suppose an image with the URL https://example-1.kxcdn.com/path/to/example.jpg is viewed from the US:

GET /path/to/example.jpg HTTP/1.1
Host: your_origin_host
X-Forwarded-Host: example-1.kxcdn.com
X-Forwarded-For: 1.2.3.4
X-Forwarded-Scheme: http
X-Pull: KeyCDN
X-Country-Code: US
Connection: close
Accept: */*
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36
Accept-Language: en-US,en

There will be different variants in the cache for the same asset. Here an example list:

/path/to/example.jpgUS
/path/to/example.jpgDE
/path/to/example.jpgFR
...

Please find here a list of the country codes.

The following nginx snippet gives you an idea how to use this on your server:

location / {
    if ($http_x_country_code ~* "US") {
        rewrite ^(.*)$ /us/$1;
    }

    ...
}

Keep in mind using this feature will decrease the cache hit ratio (CHR) as there are more variants in the cache on our edge servers.

The Cache Key Country option is a neat way to address country based caching policies without adding complex redirects in your backend. Country based caching can now be enabled in your KeyCDN Zone settings at no additional cost.

  • Share

Supercharge your content delivery 🚀

Try KeyCDN with a free 14 day trial, no credit card required.

Get started

Comments

Comment policy: Comments are welcomed and encouraged. However, all comments are manually moderated and those deemed to be spam or solely promotional in nature will be deleted.
  • **bold**
  • `code`
  • ```block```
KeyCDN uses cookies to make its website easier to use. Learn more about cookies.