How to Test Geo-Blocking: 451, APIs and Caching

Published:

12 minute read

Enver Kaya
Written by: Enver Kaya
Two plates above a GEO RULE box: a faded 200 for the allowed country, a blue 451 for the restricted one; PAGE, API, MEDIA

Geo-blocking testing is the method for verifying that the country rule on your site answers correctly to requests from allowed and restricted countries. The rule usually reads a country header that the CDN adds to the request. The decision is made either on the origin server or at the CDN edge. The trap is the cache: a response meant for a restricted country can reach an allowed one too. This post covers the expected response, the country header, API endpoints, caching and the test matrix.

The expected response

A request from a restricted country should be rejected with an error code that states the reason. The concept itself and how sites detect location are covered in our post What Is Geo-Blocking?. This post assumes you are applying a business rule, such as licensing, distribution rights or an internal policy, to your own site.

HTTP offers two codes for the job. 451 Unavailable For Legal Reasons is defined in RFC 7725 to signal that access was denied because of a legal demand. If the rule comes from a commercial decision, 403 Forbidden is also an option. Your team picks the code. The test's job is to confirm that the chosen code is the same on every surface.

ResponseWhenRisk
451 + explanation bodyThe block stems from a legal demandCan be cached
403 + explanation bodyIt is a commercial ruleMixes with other 403s
302 to an explanation pageA browser redirect is wantedThe API client gets HTML
200 + "not available in your region" textNeverTools count it as success

RFC 7725 recommends that a 451 response explain the details of the block in its body. The party enforcing the block identifies itself in a Link header with rel="blocked-by". The last row of the table is a silent failure. Monitoring tools, search engines and mobile apps treat a block page served with 200 as a successful response. If your block page returns 200, fix that first.

The CDN country header

The CDN finds the country from the visitor's IP address and adds a two-letter country code to the request it sends to the origin. Cloudflare carries this code in the CF-IPCountry header. It uses XX for a client whose country cannot be determined and T1 for a client coming from the Tor network. CloudFront passes the same information in the CloudFront-Viewer-Country header. On both CDNs the header is switched on in the settings.

The origin trusts this header. That trust only holds if the request really comes from the CDN. If the origin address is reachable directly from the internet, anyone can write and send CF-IPCountry: TR themselves. So add one more row to the test matrix: bypass the CDN and send a request straight to the origin. The expected result is a refused connection or a 403.

An unknown country is a decision too. Will you let a request with XX through or block it? This decision belongs in the rule's written spec and should be tested as a separate row. In staging, log the country code the origin receives. In the end-to-end test, that log tells you which country the CDN saw.

Faking the header locally

There is no CDN locally, so you write the country header yourself with curl. This test checks the origin logic: the allow list, the unknown country, the restricted paths. Our test server allowed only TR and DE and treated a missing country code as XX:

js
const ALLOWED = new Set(['TR', 'DE']);
const country = (req.headers['cf-ipcountry'] || 'XX').toUpperCase();
const restricted = req.url.startsWith('/film/') || req.url.startsWith('/api/v1/');
if (restricted && !ALLOWED.has(country)) {
  // 451 + explanation body + Link: <...>; rel="blocked-by"
}

A loop crossing four country codes with three paths prints the whole matrix in one go:

bash
for c in TR DE FR XX; do
  for p in /film/1 /api/v1/film/1 /api/v2/film/1; do
    printf "%s %-16s " "$c" "$p"
    curl -s -o /dev/null -w "%{http_code}\n" \
      -H "CF-IPCountry: $c" "http://127.0.0.1:8090$p"
  done
done

The restricted-country lines of our test output looked like this:

text
FR /film/1          451
FR /api/v1/film/1   451
FR /api/v2/film/1   200
XX /film/1          451
XX /api/v1/film/1   451
XX /api/v2/film/1   200

The page and the first API version answered correctly. /api/v2/ had been left out of the rule and returned 200 to the restricted country as well. We planted this bug in the test server on purpose. In real projects, too, a newly added path may never make it into the rule list. Don't write the path list in the loop by hand; generate it from your site's route table.

API and media endpoints

A restriction that works on the page does not necessarily work in the API. The mobile app, a single-page front end and partners pull data straight from the API. If blocked content can still be read there, the block on the page is only cosmetic.

In the API, the expected response is the same code as on the page, with a machine-readable body. Our test server returned this body to the restricted country:

json
{"error":"unavailable_in_region","country":"FR"}

A redirect is useless in an API. A client that receives 302 downloads the HTML of the explanation page and fails at JSON parsing. In that case a mobile app usually shows a generic "something went wrong" message.

Test media files separately. Video segments, download links and images are often served from a different domain than the page, with different cache rules. If the page returns 451 from a restricted country while the media URL opens directly, the rule only guards the front door. If you use signed URLs, also check that no signature is ever issued to the restricted country.

Cache and Vary

A shared cache stores responses by URL. If the country header is not part of the cache key, the first country's response goes to everyone. Under RFC 7725, a 451 response is cacheable by default too. The bug works both ways: an allowed country can see the block page, and a restricted country can see the content.

In our test, the origin added Cache-Control: public, max-age=300 to every response, behind a simple cache that honors the Vary header. We sent a TR request first, then an FR request:

bash
for c in TR FR; do
  curl -s -o /dev/null -D - \
    -H "CF-IPCountry: $c" \
    http://127.0.0.1:8091/film/4 \
    | grep -iE "^(HTTP|x-cache|vary)"
done

Without Vary, the FR request got 200 OK and X-Cache: HIT. The restricted country saw the content stored for TR. When the order was reversed, the TR request got 451 from the cache. When the origin sent Vary: CF-IPCountry, the two countries got separate entries: TR 200, FR 451, both MISS on the first request.

Vary tells the cache which request headers a response depends on. A cache split by country keeps a separate copy per country. CDNs don't always translate this header into their own cache key. In CloudFront, if you add the country header only to the origin request policy, the origin receives the header but the cache key stays the same. For it to enter the key, the header must be added to the cache policy. On your own CDN, verify this not from the docs but with back-to-back requests from two countries.

A test matrix with proxies

The local test checks the origin logic, not the CDN. For an end-to-end test, requests must really come from IPs in allowed and restricted countries. A country-targeted Residential Proxy does this job: you set the exit to an allowed country first, then to a restricted one. We explain the background of country selection in our post How Does Country and City Targeting Work in a Proxy?.

bash
curl -s -o /dev/null -D - \
  -x http://user:pass@pr.proxynet.io:8000 \
  https://staging.example.com/film/1 \
  | grep -iE "^(HTTP|location|link|vary|cache-control|age)"

With an HTTP proxy, the proxy resolves the domain name. The CDN also picks the edge server based on the exit IP, not your office. We showed how to read the edge server from the response headers in How to Test a Site With a Türkiye IP. Compare each scenario once against the staging log as well. The CDN's location database may not give the same country as the proxy provider's for every IP. If the country code in the log is not the one you expected, the result is measuring the mapping, not the rule.

ScenarioRequestExpected
Allowed country, pageTR exit200
Restricted country, pageFR exit451 or 403, explanation body
Restricted country, APIFR exit, /api/Same code, JSON error
Restricted country, mediaFR exit, direct file URLSame code or no signature
Cache, allowed firstTR, then FRFR gets 451, not HIT
Cache, restricted firstFR, then TRTR gets 200
Unknown countryXX (local test)The response the written rule chose
Direct to originFake country headerConnection refused or 403

For the cache rows, send the two requests close together, or the entry expires and the bug stays hidden. Test multi-step flows with a sticky session, from the same IP. The duration is chosen between 1 and 60 minutes.

Troubleshooting

Most bugs come from three places: the rule list, the cache key and the country mapping.

  • A restricted country sees content, X-Cache: HIT. The country is not in the cache key. Add Vary or put the country header in the CDN's cache policy.
  • An allowed country sees the block page. The same problem in reverse. A 451 or 403 response was stored in the shared cache.
  • Page blocked, API open. The new path never made it into the rule list. Generate the path list from the route table.
  • Every country gets 451. The country header isn't reaching the origin, and the rule treats XX as blocked. Check that the header is enabled in the CDN settings.
  • The proxy exits in a restricted country but the site allows it. The CDN may map that IP to another country. Check the country code in the staging log.

How we tested

We ran the commands on December 3, 2026 on Windows 11: Node 24.22.2 and curl 8.23.0. The setup had three local parts. The first was a test server that read the CF-IPCountry header and returned 451 to every country except TR and DE. The second was a simple cache we wrote ourselves, keyed on the URL plus the values of the Vary headers. The third was an HTTP proxy requiring user:pass, which checked the -x syntax. Real country exits and a real CDN were not part of this test.

The bottom line

Test the rule locally with the country header first, then behind the CDN with country-exit proxies. Keep page, API and media in the same matrix. For long-running regression tests, a ISP Proxy keeps the same address. Above all, test the cache: if the country is not in the key, even a correct rule answers the wrong country.

Sources

Frequently asked questions

Should I return 451 or 403 for geo-blocking?

Either works. RFC 7725 defines 451 for access denied because of a legal demand. If the rule comes from a commercial decision such as licensing, 403 is also an option. Whichever code you choose, page, API and media must return the same code, and the body should explain the reason. Test that consistency in separate rows.

Is faking the country header with curl enough?

No. Faking the header only checks the origin logic: the allow list, the unknown country and the restricted paths. The CDN's IP-to-country lookup, edge server selection and cache key are not part of that test. For those three, the request must really come from an IP in that country, through the CDN. Run both layers together.

Does the Vary header slow down the cache?

Yes, it can lower the hit rate. Vary: CF-IPCountry makes the cache store a separate copy per country, so more requests reach the origin for the same URL. There are two options: accept per-country copies, or make the restriction decision before the cache, at the CDN edge. Whichever you choose, verify the result with back-to-back requests from two countries.

What should happen to a request with an unknown country (XX)?

Whatever your rule's written spec says. Cloudflare uses XX for a client whose country cannot be determined and T1 for the Tor network. Treating it as blocked prevents restricted content from leaking; treating it as allowed doesn't lock out users whose location can't be found. The decision belongs to the product team. Add separate rows for XX and T1 and write down the expected response.

How do I block a fake country header sent straight to the origin server?

By opening the origin only to requests from the CDN. If the origin is reachable directly from the internet, anyone can send CF-IPCountry: TR and the rule cannot tell the difference. A firewall that allows the CDN's IP ranges, or a secret header you add to CDN-to-origin requests, closes that path. In the test, send a request straight to the origin address and confirm it is refused.

Do I need a separate IP for each country in the test?

Yes, for the end-to-end test. The CDN finds the country from the IP the request comes from, so allowed and restricted countries need separate exits. For a single-page check, an IP that changes on every request is enough. If you are testing a login or a multi-step flow, choose a sticky session. Compare the result with the country code in the staging log.

Ask ChatGPTAsk Claude