Skip to content

Blocking Facebook in nginx

  • by

How to block Facebook complete from your site? It is easy using geoip2 module in nginx.

In Debian / Ubuntu install libnginx-mod-http-geoip2 and we need GeoIP2 database with ASN numbers:

  • Or you can register on IPinfo.io and use IP to Country + ASN database (country_asn.mmdb)

I am using second option in this example. For the first option, just change mmdb file and and path in this config snippets:

# this block goes to http { part of config, for example
# /etc/nginx/conf.d/geoip.conf

    geoip2 /usr/share/GeoIP/country_asn.mmdb {
         # if you have some database update script, you can configure auto reload
         # auto_reload 1h;
         $geoip2_asn asn;
         $geoip2_as_name as_name;
         $geoip2_continent continent;
         $geoip2_continent_name continent_name;
         $geoip2_country country;
    }
# put this in location
# Block Facebook ASN AS32934
    if ($geoip2_asn = "AS32934") {
        return 402;
    }

Leave a Reply

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