Security

Block by Country Code

By 27th February 2015June 9th, 2017No Comments

Sometimes, your site may fall victim to country specific attacks or crawl bots. Blocking access to your server for a specific country is very straightforward, with a simple edit to your domain's ___general/example.com.conf file

Eg. To block the country with country code aa

if ($geoip_country_code ~* (aa)) {
  return 403;
}

You can use Perl REGEX to match multiple country codes within a single statement.

Eg. To block multiple countries

if ($geoip_country_code ~* (aa|bb)) {
  return 403;
}

Alternatively, if you want to give a discrete message, rather than an outright block (perhaps to allow for humans to contact you if there is an error), then a rewrite would be more suitable.

Eg. To redirect all requests to a static HTML page for visitors from country code aa

if ($geoip_country_code ~* (aa)) {
  rewrite .* /no-countries.html last;
}

Then just make a normal HTML file in /no-countries.html with whatever message you would like to pass to the user.