Josheli
  • Home
  • Blog
    • Knob
    • Running
    • Soccer
    • Technology
  • About
Knob , Technology

Block Website Visitors by Country using GeoIP on Nginx

by dv January 4, 2016 No Comments

These tech posts on how I did something are mostly just a diary of my own server maintenance, so that in 11 months, when the website I googled is no longer found, I can remember what I did, why, where and how.

Familyvance.com was getting slammed by Russian bots with the former Soviets comprising more than 50% of all visits. Really, there’s no reason for anybody in Russia to visit familyvance.com, so I decided to just block the whole country. Sure, enterprising Slavs could use a VPN or Tor or whatever, but for the common use case, a country block should do.

So basically, I did this, also found here.

1. Make sure nginx has geoip module compiled in
nginx -V

2. Install GeoIP alternate package. This contrib package replaces the standard GeoIP database, and includes a cronjob to update the IP database files from maxmind automatically.
sudo apt-get install geoip-database-contrib

3. Update http block in nginx.conf. The first block is the pertinent bit, the second block ($exclusions), is in case you want to have exceptions within the country you blocked.
geoip_country /usr/share/GeoIP/GeoIP.dat;
map $geoip_country_code $allowed_country {
default yes;
RU no;
}
geo $exclusions {
default 0;
10.8.0.0/24 1;
}

4. Update server block in vhost familyvance.conf
if ($allowed_country = yes) {
set $exclusions 1;
}
if ($exclusions = "0") {
return 444;
}

5. Restart nginx
sudo service nginx restart

Sorry Boris.

Related Content:

  • Installing Let's Encrypt SSL on Nginx and WordPress by Dv December 19, 2015 Here's how I installed the free Let's Encrypt SSL certificate on Nginx to use with this WordPress site. I used…
  • You may remember me from such posts as the last one by Dv September 11, 2006 Don't really feel like prattling on about my running, so I'll just say I ran the sparkler this morning, cutting…
  • Video Episode 4: Running, Hiking, Fishing, and… by Dv July 7, 2020 These blog posts and vlog episodes feel like they're becoming rote and routine, and I'm already tired of producing them…
  • Running Dreams by Dv July 27, 2007 Does anyone else dream about running? I mean actual nocturnal dreams of six-dimensional weirdness, not daydreams of greatness. Last week…
  • Episode 7: Winter Running and My Fastest 5K in 13 Years by Dv February 7, 2022 The latest installment of my running vlog, Josheli Running, is up on YouTube. In Episode 7: Winter Running and My…
It's only fair to share...Share on facebook
Facebook
Share on twitter
Twitter
Share on email
Email
  • Previous Come Ski With Me10 years ago
  • Next PHP, JSON, Strings, Integers, Mysqlnd and Matching Environments10 years ago

Leave a Reply

Your email address will not be published. Required fields are marked *

Popular Posts

  • Josheli, What Happened? (56,260)
  • Stupidly Simple, Static, Startpage for Self-hosted Services (33,221)
  • Three Saturdays (31,726)
  • Running a Plex Media Server on an Old Laptop (27,224)
  • Simple Google Photos: A WordPress Plugin (23,698)

Random Read

Doing something right?
As long as I'm up and "bloggin'", might as well add another one, eh? We…

Read More

Google Photo
Google Photo
Google Photo
Google Photo

Social Things

  • Family Vance
  • Texas Longhorns News

RSS From Familyvance

2025 Josheli. Donna Theme powered by WordPress