How to Block Countries/IP Ranges at the Firewall Level

For services with a clearly defined legitimate audience, blocking traffic from entire countries or IP ranges at the firewall level can reduce unwanted traffic and certain classes of attacks. This guide covers implementation and its real limitations.

When Country/IP Blocking Makes Sense

  • Your legitimate user base is genuinely concentrated in specific regions
  • You're seeing consistent attack traffic from specific IP ranges/regions with no legitimate use case there
  • Regulatory or business requirements dictate restricting access from certain regions

Important Limitations to Understand First

IP-based geolocation isn't perfectly accurate, and determined attackers/VPN users can trivially bypass country blocking — this is a blunt instrument that reduces volume from unsophisticated sources, not a robust security control against a determined, capable attacker.

Step 1 — Obtain a GeoIP Database

sudo apt install geoipupdate -y

Configure with a GeoIP database license (several free and paid options exist) to get country-to-IP-range mapping data.

Step 2 — Blocking with iptables and ipset (Efficient for Large Lists)

sudo apt install ipset -y
sudo ipset create blocked_countries hash:net

Populate the ipset with IP ranges for countries you want to block (derived from your GeoIP database), then reference it in an iptables rule:

sudo iptables -A INPUT -m set --match-set blocked_countries src -j DROP

Step 3 — Blocking at the Nginx Level (Application-Layer Alternative)

See How to Set Up Nginx Access Control by IP Address or Geolocation for the GeoIP2 module-based approach directly in Nginx, appropriate if you specifically want to block at the web server layer rather than the full firewall.

Step 4 — Keep the GeoIP Database Updated

sudo geoipupdate

IP-to-country mappings change over time as address allocations shift — schedule regular updates (via cron) to keep blocking accuracy reasonably current.

Testing Your Configuration

Use a VPN or proxy service with an exit point in a blocked country to verify the block is actually working as intended before relying on it.

Avoiding Overly Broad Blocking

Country-level blocking is inherently coarse — you'll inevitably block some legitimate users (travelers, VPN users, legitimate business connections) alongside unwanted traffic; weigh this trade-off honestly against your actual security/business needs.

Combining with Other Security Measures

Country blocking works best as one layer among several, not a standalone security solution — combine with rate limiting (see How to Rate Limit an API with Nginx), fail2ban, and standard application security practices.

Common Errors

Legitimate users unexpectedly blocked — verify your GeoIP database is current, and consider whether the specific blocking policy is too broad for your actual legitimate audience's geographic diversity.

Continue Reading

Browse more articles in Networking & DNS.

  • block country firewall, geoip blocking, ip range firewall block, geographic access restriction
  • 0 Bu dökümanı faydalı bulan kullanıcılar:
Bu cevap yeterince yardımcı oldu mu?

İlgili diğer dökümanlar

DNS Fundamentals: A, AAAA, CNAME, MX, TXT & NS Records Explained

DNS translates human-readable domain names into the information servers actually need — IP...

How to Point a Domain to Your VPS (A/AAAA Records)

Before your website is reachable at yourdomain.com instead of a raw IP address, you need to...

How to Configure MX Records for Email Delivery

MX (Mail Exchange) records tell the internet which servers handle incoming email for your domain....

How to Use CNAME Records Correctly

CNAME records let you alias one domain name to another, but they come with important restrictions...

How to Configure a Static IP on a Linux VPS

Most VPS providers assign a static (unchanging) IP address by default, but understanding how...