DEV Community

Cover image for Block Countries and Continent : Apache2
Tikam Singh Alma
Tikam Singh Alma

Posted on • Edited on

Block Countries and Continent : Apache2

install module

sudo apt install libapache2-mod-geoip

enable module

sudo a2enmod geoip

Method 1 โ€“ Server Wide Configuration

sudo nano /etc/apache2/mods-available/geoip.conf

edit conf

<IfModule mod_geoip.c>
  GeoIPEnable On
  GeoIPDBFile /usr/share/GeoIP/GeoIP.dat
</IfModule>
Enter fullscreen mode Exit fullscreen mode

Method 2 โ€“ Virtual Host Configuration

sudo nano /etc/apache2/sites-available/virtualhost.conf

Add the following below theย ServerAliasย directive.

GeoIPEnable On
GeoIPDBFile /usr/share/GeoIP/GeoIP.dat
GeoIPScanProxyHeaders On
Enter fullscreen mode Exit fullscreen mode

Manage Restrictions

Block Certain Countries

Create or open the .htaccess file which is inside your web root directory and add the following snippet to block countries.

SetEnvIf GEOIP_COUNTRY_CODE UA BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE VN BlockCountry
Deny from env=BlockCountry
Enter fullscreen mode Exit fullscreen mode

The above configuration will block requests from the above 2 countries. You can include as per your wish.

Allow Certain Countries

Create or open the .htaccess file which is inside your web root directory and add the following snippet to allow countries.

SetEnvIf GEOIP_COUNTRY_CODE US AllowCountry
SetEnvIf GEOIP_COUNTRY_CODE CA AllowCountry
Deny from all

Allow from env=AllowCountry
Enter fullscreen mode Exit fullscreen mode

Blocking a client based on country

This example show you how to block clients based on the country code that GeoIP sets.

GeoIPEnable On
GeoIPDBFile /path/to/GeoIP.dat

SetEnvIf GEOIP_COUNTRY_CODE CN BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE RU BlockCountry
# ... place more countries here

Deny from env=BlockCountry
Enter fullscreen mode Exit fullscreen mode

Allowing clients based on country

This example show you how to allow only clients from specific countries.

GeoIPEnable On
GeoIPDBFile /path/to/GeoIP.dat

SetEnvIf GEOIP_COUNTRY_CODE US AllowCountry
SetEnvIf GEOIP_COUNTRY_CODE CA AllowCountry
SetEnvIf GEOIP_COUNTRY_CODE MX AllowCountry
# ... place more countries here

Deny from all
Allow from env=AllowCountry
Enter fullscreen mode Exit fullscreen mode

GeoIP options

GEOIP_ADDR
GEOIP_CONTINENT_CODE
GEOIP_COUNTRY_CODE
GEOIP_COUNTRY_NAME
GEOIP_REGION
GEOIP_REGION_NAME
GEOIP_CITY
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
webhive profile image
Roman Tataurov

This module is deprecated. See details here blog.maxmind.com/2020/06/retiremen...