NGINX 按国家/地区阻止并排除机器人

NGINX block by country and exclude bots

我想阻止来自除一个国家以外的所有国家的流量,并将搜索机器人添加到例外。我正在尝试这个:

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;
   }
...
}

但它对我不起作用。

感谢@RichardSmith,这是最终代码:

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;
   }
...
}