【问题标题】:NGINX block by country and exclude botsNGINX 按国家阻止并排除机器人
【发布时间】:2021-05-15 10:02:34
【问题描述】:

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

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

但它对我不起作用。

【问题讨论】:

  • 请注意,IP 地理位置不准确。您将禁止部分您不想禁止的用户。
  • 如果你想连接“I”和“G”,第二个set应该是set $blocked "${blocked}G";

标签: nginx bots geoip


【解决方案1】:

感谢@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;
   }
...
}

【讨论】:

    猜你喜欢
    • 2018-08-20
    • 1970-01-01
    • 2020-10-03
    • 2013-10-20
    • 2014-10-05
    • 1970-01-01
    • 1970-01-01
    • 2020-02-09
    • 1970-01-01
    相关资源
    最近更新 更多