nginxbotsgeoip

NGINX block by country and exclude bots


I want to block traffic from all countries except one and add search bots to the exceptions. I'm trying this:

geoip_country /usr/share/GeoIP/GeoIP.dat;
map $geoip_country_code $allowed_country {
   default no;
   RU yes;
}

server {
   listen 443 ssl http2;
   server_name _default;
   if ($allowed_country = no) {
      set $blocked I;
   }
   if ($http_user_agent !~* (google|bing|yandex|msnbot|applebot)) {
      set $blocked G;
   }
   if ($blocked = IG){
      return 444;
   }
...
}

But it don't working for me.


Solution

  • Thanks to @RichardSmith, here is final code:

    geoip_country /usr/share/GeoIP/GeoIP.dat;
    map $geoip_country_code $allowed_country {
       default no;
       RU yes;
    }
    
    server {
       listen 443 ssl http2;
       server_name _default;
       if ($allowed_country = no) {
          set $blocked I;
       }
       if ($http_user_agent !~* (google|bing|yandex|msnbot|applebot)) {
          set $blocked "${blocked}G";
       }
       if ($blocked = IG){
          return 444;
       }
    ...
    }