Testing Geoblocking Rules on Linux Servers Before Real Customers Get Locked Out

Last updated: November 27, 2026 9 mins read

Testing geoblocking rules on Linux servers

Geoblocking, the restriction of access to a website or service based on the country in which the visitor's IP address is located, is implemented on Linux servers either in the packet filter, using nftables or iptables sets generated from a GeoIP database, or in the web server, most often nginx with the third party ngx_http_geoip2_module. GeoLite2, the free database published by MaxMind, Inc., is the most common data source for both methods.

GeoLite2 Country, which MaxMind distributes in the binary MMDB format and as CSV files, assigns IPv4 and IPv6 networks to ISO 3166 country codes and is updated twice weekly, on Tuesdays and Fridays. Between releases, networks are reassigned to other countries when address blocks are transferred between organizations, between regional internet registries such as ARIN and RIPE NCC, or put into service by a different network operator.

The example configuration is that of an online shop which ships only to Canada and the United States, hosted on Debian 12 ("bookworm") with nftables 1.0.6 and nginx 1.22.1. Country lookups in nginx are handled by version 3.4 of the geoip2 module from the libnginx-mod-http-geoip2 package, since the geoip module distributed with nginx reads only the legacy GeoIP format that MaxMind no longer publishes.

Age of the GeoIP database

The geoipupdate program reads a MaxMind account ID and license key from /etc/GeoIP.conf. When the key is revoked or replaced without a change to that file, every update fails, and the database in /var/lib/GeoIP stays at the version of the last successful download.

The modification date of the database file, the output of a manual update and the list of systemd timers show whether updates are being installed.

ls -l --time-style=long-iso /var/lib/GeoIP/GeoLite2-Country.mmdb
geoipupdate -v
systemctl list-timers | grep -i geoip

Individual addresses are looked up with mmdblookup, from the mmdb-bin package. Addresses in the 192.0.2.0/24, 198.51.100.0/24 and 203.0.113.0/24 ranges, here and in the rest of the article, are documentation examples defined in RFC 5737 and are replaced with real ones.

mmdblookup --file /var/lib/GeoIP/GeoLite2-Country.mmdb --ip 203.0.113.45 country iso_code

Log only rule in nftables

In the first version of the rule, traffic is not dropped. Packets from addresses outside the permitted sets are written to the kernel log with the prefix geo-would-drop and counted, and then continue to the accept policy of the chain. These entries are potential blocks, connections that the rule would refuse after enforcement. The ruleset is kept in /etc/nftables.d/geo.nft.

table inet geo
delete table inet geo

table inet geo {
  set allow4 { type ipv4_addr; flags interval; }
  set allow6 { type ipv6_addr; flags interval; }

  chain input {
    type filter hook input priority filter - 10; policy accept;
    tcp dport { 80, 443 } ct state new jump check
  }
  chain check {
    ip saddr @allow4 return
    ip6 saddr @allow6 return
    limit rate 20/second log prefix "geo-would-drop "
    counter
  }
}
include "/etc/nftables.d/geo-allow*.nft"

Loading the file creates the table, deletes it, and creates it again with the rules and the addresses from the geo-allow files, all in one transaction, which according to the nftables wiki is applied by the kernel atomically.

The CSV edition of GeoLite2 identifies countries by geoname_id, a number from the GeoNames database. Canada is 6251999 and the United States 6252001. For networks with an empty geoname_id, the registered_country_geoname_id in the third column is used instead.

awk -F, '{c = ($2 != "") ? $2 : $3} c==6251999 || c==6252001 {print $1}' GeoLite2-Country-Blocks-IPv4.csv | paste -sd, - | sed 's/.*/add element inet geo allow4 { & }/' > /etc/nftables.d/geo-allow-v4.nft
nft -c -f /etc/nftables.d/geo.nft && nft -f /etc/nftables.d/geo.nft

The ruleset is reloaded only if nft -c, the check mode of nft, accepts it.

For IPv6 the same two commands are run with GeoLite2-Country-Blocks-IPv6.csv and the set allow6. Google reports that close to half of its users reach it over IPv6.

The kernel log is limited to 20 entries per second for all sources together, because the limit statement, as described in the nft manual page, stops matching once the rate is exceeded and the log statement after it in the same rule is then skipped. A burst of connections from a single crawler or scanner can take up the whole allowance, and entries for other addresses are then missing, whereas the counter in the following rule has no limit and records the total number of new TCP connections that would have been dropped.

journalctl -k --grep 'geo-would-drop' --since '24 hours ago' | wc -l
nft list chain inet geo check

Country lookup in nginx

Behind a content delivery network such as Cloudflare or Fastly, the lookup takes place in nginx, with the realip module reading the client address from X-Forwarded-For.

The ngx_http_geoip2_module, an open source nginx module published on GitHub, reads MaxMind DB files and stores the result of each lookup in an nginx variable, which can then be used in map blocks, log formats and conditions, and the geoip2, map and log_format blocks are placed in the http context. The module is not part of the nginx source and on Debian is loaded from /etc/nginx/modules-enabled after installation of the libnginx-mod-http-geoip2 package, while the realip module is compiled into the nginx package. The auto_reload parameter makes the module check the database file for changes at a set interval and load a new release without a restart of nginx.

ls /etc/nginx/modules-enabled/ | grep geoip2
nginx -V 2>&1 | grep -o with-http_realip_module
geoip2 /var/lib/GeoIP/GeoLite2-Country.mmdb {
  auto_reload 6h;
  $geo_country default=ZZ country iso_code;
}
map $geo_country $geo_block { default 1; CA 0; US 0; }
log_format geo '$remote_addr $geo_country block=$geo_block "$request" $status';

The add_header directive of nginx adds a field to the response headers, and its always parameter extends this to error responses such as 403. In the server block it is combined with a separate access log in the geo format. Locations with add_header directives of their own do not inherit it from the server block.

access_log /var/log/nginx/geo.log geo;
add_header X-Geo-Decision "$remote_addr $geo_country $geo_block" always;

Analysis of logged decisions

After three to five days, the awk command below counts the potential blocks in geo.log by country code.

awk '$3=="block=1" {print $2}' /var/log/nginx/geo.log | sort | uniq -c | sort -rn | head -20

For /checkout, /cart and /account, which search engine crawlers rarely request, a second count gives a figure much closer to the number of customers who would have received the 403 page.

grep 'block=1' /var/log/nginx/geo.log | grep -cE '"(GET|POST) /(checkout|cart|account)'

ZZ is not an assigned ISO 3166 code, and in this configuration it marks addresses that the database could not place in any country. With default 1 in the map, these requests would be blocked together with those from countries outside the list.

Tests from Canada and other countries

In the example, requests are sent through Canadian proxies on residential Cable/DSL and Cellular connections in Halifax, Edmonton and Winnipeg.

The script uses curl. Since version 7.84.0, the -w option of curl can print a response header with the %header{} variable.

#!/bin/bash
# ./geo-probe.sh proxies.txt urls.txt
while read -r label proxy; do
  while read -r url; do
    res=$(curl -s -m 20 -x "$proxy" -o /dev/null \
      -w '%{http_code} %{size_download} %header{x-geo-decision}' "$url" </dev/null)
    rc=$?
    if [ $rc -eq 0 ]; then echo "$label $res $url"
    elif curl -s -m 15 -x "$proxy" -o /dev/null https://example.com </dev/null; then echo "$label NO-ANSWER curl_exit=$rc $url"
    else echo "$label PROXY-FAIL curl_exit=$rc $url"; fi
  done < "$2"
done < "$1"

During the log only phase the output has the following form, with the status, the response size, and the address, country and decision that nginx returned in the header of the same request, which keeps the address correct with proxies that rotate exits.

ca-halifax 200 48211 203.0.113.17 CA 0 https://shop.example/checkout
de-berlin 200 48211 198.51.100.62 DE 1 https://shop.example/checkout
ca-edmonton PROXY-FAIL curl_exit=28 https://shop.example/checkout

The exit in Germany still receives status 200, as nothing is refused yet, and the value 1 in the last field marks a potential block. Canadian carriers other than those of the tested exits are not covered.

A NO-ANSWER or PROXY-FAIL line in the log only phase is a failed connection and not a geoblock, since no traffic is dropped yet. In curl, exit code 28 indicates a timeout and codes 7 and 97 a failed connection to or through the proxy, and such lines are repeated through another exit before a conclusion is drawn.

After enforcement, a status of 403 with block=1 in the header is a block by this rule, while a 403 without the header comes from the CDN or from a location that does not pass the header on. A status of 200 with a much smaller response size usually indicates a block page served with the wrong status.

Limitations

The country assigned to an address by a GeoIP database is an estimate, and MaxMind publishes accuracy figures for GeoLite2 that vary from one country to another. Further limitations of the configuration described here include the following:

  • Visitors using a VPN, Tor or a proxy appear in the country of their exit point, and geoblocking is therefore a policy or licensing control rather than a form of authentication.
  • Behind a content delivery network, the packet filter on the origin server receives connections only from the network's edge nodes, and the nftables rule would drop edge nodes outside Canada and the United States, so on such servers only the nginx check is used.
  • A 403 page cached at the edge may be served to visitors in permitted countries.

For requests from the ranges listed in set_real_ip_from, the realip module takes the client address from the X-Forwarded-For header. The range 198.51.100.0/24 in the example is a documentation placeholder and is replaced with the IPv4 and IPv6 ranges that the content delivery network publishes for its edge nodes, since any other source in the list could set the header to an arbitrary address.

# placeholder: replace with the CDN's published edge ranges
set_real_ip_from 198.51.100.0/24;
real_ip_header X-Forwarded-For;
real_ip_recursive on;

The caching of block pages is avoided by serving the page from an internal location that adds the header Cache-Control no-store, defined in RFC 9111, to the response.

error_page 403 /geo-blocked.html;
location = /geo-blocked.html {
    internal;
    add_header Cache-Control "no-store" always;
    add_header X-Geo-Decision "$remote_addr $geo_country $geo_block" always;
}

Deployment and rollback

Before enforcement, the active ruleset is written to /root/nft-before-geo.nft with flush ruleset as its first line, and systemd-run starts a timer named geo-rollback that loads the file after ten minutes unless it is stopped first. geo.nft is not yet included in /etc/nftables.conf, which nftables.service loads at boot.

{ echo 'flush ruleset'; nft list ruleset; } > /root/nft-before-geo.nft
systemd-run --unit=geo-rollback --on-active=10min /usr/sbin/nft -f /root/nft-before-geo.nft

The last two lines of the check chain are replaced with counter drop, and the file is checked and loaded.

nft -c -f /etc/nftables.d/geo.nft && nft -f /etc/nftables.d/geo.nft

After enforcement, a request from a refused exit ends in NO-ANSWER with curl exit code 28, or 56 or 97 when the proxy gives up first, because the dropped SYN receives no answer while the proxy still reaches example.com, and the counter of the check chain rises. If the probe results are correct, the timer is stopped with systemctl stop geo-rollback.timer.

In nginx, enforcement consists of a single if block containing a return directive, one of the two uses of if that the nginx wiki article "If is evil" describes as safe, and the X-Geo-Decision header is taken out of the server block and the block page location after the last probe.

if ($geo_block) {
    return 403;
}

With /etc kept in git by etckeeper, the if block is added to one virtual host with low traffic for 48 hours before the others and removed, if necessary, with git revert, nginx -t and a reload.

Checks after enforcement

nft list chain inet geo check shows the packets and bytes counted since the ruleset was loaded, and the value is reset by each reload.

After each run of geoipupdate, the awk count and the probe script are run again.

Souvic Chakraborty, Ph.D.

With a background in AI and a passion for clear technical communication, I enjoy breaking down complex tools and processes. Exploring new software and sharing insights is a key focus.

Run a gaming, tech, or AI site? Write for us or swap links →

Further Reading

Collecting Public Web Data Without Wrecking Your Tools or Your Reputation

How community tool and bot builders can read public web data without getting blocked: narrow scope, caching, rate limits and a kill switch.