• Home
  • The Song
  • The Avatar
  • The Cat
  • Contact the Cat

Gyp the Cat dot Com

Blocking Countries on Nginx without the GeoIP Module
internet

Blocking Countries on Nginx without the GeoIP Module

Sometimes blocking entire swathes of the world is the way to go if you truly want a defense in depth approach on your web apps, for instance do your administrators truly need to log on from every country on Earth? As website administrators we get a feel for where our legitimate traffic is coming from and where we see nefarious connections from.

So this tutorial will guide you through blocking, and in some cases specifically allowing traffic from various sources. So we will be blocking countries using IP ranges in CIDR range (which Nginx can happily deal with), this is instead of using the GeoIP module. I will give you some examples of configuration examples and if you have any questions feel free to leave a comment!

A couple of words of warning:

  • Methods of determining country of origin through IP addresses are not always entirely accurate, country blocking and allowing is not always fool proof
  • Putting these measures on your server has an overhead, and dependent on usage it could be a high overhead, you want to give your server the smallest number of checks

After scratching my head for a while and trying to use multiple sources I decided to make it easy, and I’ve created a little site with some Nginx Country IP Blocks. Check it out!

SSH onto your Linux server as usual, I’ll be using Ubuntu for these instructions but the Nginx config should be the same.

We will need to make sure we have unzip installed, it possibly will be:

apt-get install unzip

We will put the files into an appropriate directory, namely in our Nginx configuration tree.

cd /etc/nginx

Create a directory for our country database:

mkdir country-cidr
cd country-cidr

Download the country databases:

wget http://firewalliplists.gypthecat.com/lists/nginx/nginx-countries.conf.zip

Unzip the file:

unzip nginx-countries.conf.zip

You should now have a directory full of some configuration files for nginx, labelled as both allow and deny.

Now we have to decide how we want things to work. Say for instance we have the following configuration for our Nginx virtual server:

server
{
listen 80;
server_name www.companya.com companya.com;
root /webhost/companya.com/httpdocs/;
access_log /webhost/companya.com/logs/access.log combined;
error_log /webhost/tld.co.uk/logs/error.log;
index index.php index.html index.htm;

location /admin/
    {
    # Do something here
    }

location /europe/
    {
    # Do something here
    }

location /mexico/
    {
    # Do something here
    }

location /world/
    {
    # Do something here
    }

}

We have the following use case:

  • Admins should only ever log on from Germany
  • Users in France, Germany, Spain and Italy are the only ones who should see the content in the Europe directory
  • Users in Mexico are the only ones able to view the Mexico directory
  • Everyone in the world can view the World directory except for Canada

So if we’ve extracted the files to the directories above our config should look like the following:

server
{
listen 80;
server_name www.companya.com companya.com;
root /webhost/companya.com/httpdocs/;
access_log /webhost/companya.com/logs/access.log combined;
error_log /webhost/tld.co.uk/logs/error.log;
index index.php index.html index.htm;

location /admin/
    {
    include country-cidr/DE-allow.conf;
    deny all;
    # Do something here
    }

location /europe/
    {
    include country-cidr/FR-allow.conf;
    include country-cidr/DE-allow.conf;
    include country-cidr/ES-allow.conf;
    include country-cidr/IT-allow.conf;
    deny all;
    # Do something here
    }

location /mexico/
    {
    include country-cidr/MX-allow.conf;
    deny all;
    # Do something here
    }

location /world/
    {
    include country-cidr/CA-deny.conf;
    allow all;
    # Do something here
    }

}

Give the Nginx service a restart or a reload:

service nginx reload

And that’s it! These IP files do get updated daily, so you may need to keep an eye on how up to date your versions are, a tutorial on how to automate this process will follow soon.

Related

Written by gyp - April 25, 2015 - 9558 Views
Tags | internet, linux, nginx, security, ubuntu

You Might Also Like

Configuring Suite B, VPN-A and VPN-B in IPSec with Strongswan

October 13, 2015

How Secure is Your Password? Rainbow Tables Online

August 3, 2011

Steam & Valve IP Ranges

January 8, 2016

4 Comments

  • janis October 22, 2016 at 4:27 pm

    download link for the list is offline!

    Reply
    • gyp October 22, 2016 at 4:42 pm

      Hi Janis,

      I’ve just had a try now and it’s working ok from here.

      This one? http://firewalliplists.gypthecat.com/lists/nginx/nginx-countries.conf.zip

      Gyp

      Reply
      • janis October 22, 2016 at 4:45 pm

        hello, yes I get 404!

        Reply
        • gyp October 23, 2016 at 1:06 pm

          That is really strange, I’ve tried it on my home and office connections and it downlods ok. I’ve also tried it on a couple of servers and it works fine there too.

          Are you using a proxy or anything like that?

          Gyp

          Reply

    Leave a Reply to janis Cancel reply

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

    Previous Post
    Next Post

    Latest Posts

    • How to Convert CSV to Parquet Easily with Python on Linux Shell
    • Kusto Geolocation IP Lookup
    • Monitoring Tor Usage in Azure Sentinel, ASC, MDATP and ALA
    • HTTP to HTTPS Redirect on Azure CDN
    • Strongswan IPSec (Including Cryptomap) to Microsoft Azure Virtual Network Gateway
    • Black Ops 3 NAT Type Strict & PS4 NAT Type 3 with pfSense Fixed!
    • Sorry for the lack of posts
    • How to Block Internet Access with Group Policy (GPO)
    • Enforcing Microsoft Office 365 and Azure Tennancy with McAfee Web Gateway (MWG)
    • Scanning Subnet for Issuing Certificate Authority with OpenSSL

    Top Posts & Pages

    • How to Block Internet Access with Group Policy (GPO)
      How to Block Internet Access with Group Policy (GPO)
    • How to Configure Windows 2012 NPS for Radius Authentication with Ubiquiti Unifi
      How to Configure Windows 2012 NPS for Radius Authentication with Ubiquiti Unifi
    • Kusto Geolocation IP Lookup
      Kusto Geolocation IP Lookup
    • Tinyproxy A Quick and Easy Proxy Server on Ubuntu
      Tinyproxy A Quick and Easy Proxy Server on Ubuntu
    • Monitoring Tor Usage in Azure Sentinel, ASC, MDATP and ALA
      Monitoring Tor Usage in Azure Sentinel, ASC, MDATP and ALA
    • How to DNSPerf on Ubuntu 14.04 with Installation and Quick Start
      How to DNSPerf on Ubuntu 14.04 with Installation and Quick Start
    • How to Add Different Disclaimers using alterMIME and Postfix based on Domain
      How to Add Different Disclaimers using alterMIME and Postfix based on Domain
    • Blocking Countries on Nginx without the GeoIP Module
      Blocking Countries on Nginx without the GeoIP Module
    • How to Enable Squid Anonymous Stealth Mode
      How to Enable Squid Anonymous Stealth Mode
    • Configuring Suite B, VPN-A and VPN-B in IPSec with Strongswan
      Configuring Suite B, VPN-A and VPN-B in IPSec with Strongswan

    Tags

    apache2 azure azure log analytics blops business centos cheating cissp cloudflare cryptography dns game google gyp internet iphone ipsec isc linux mac marketing microsoft mw2 mx mysql nginx pfsense postfix proxy ps3 qualification radius revision security seo smtp socks squid ssh strongswan tinyproxy ubuntu windows 2012 wordpress xdecrypt.com
    Gyp the Cat dot Com

    Some rights retained Gyp the Cat Dot Com