Nginx geoipblocking 和允许 LAN IP

joh*_*ohn 2 local-area-network nginx ip-blocking whitelist geoip

除了白名单国家和局域网之外,我想使用 geoip 阻止 IP。第一部分完美无缺,第二部分没有。Somwehere 搜索互联网我找到了代码 LH(本地主机)和 LN(本地网络),但它们都不起作用。

我正在使用标准的 howto 代码进行阻塞。

nginx.conf 中相关的 http {} 部分:

geoip_country /usr/share/GeoIP/GeoIP.dat;
   map $geoip_country_code $allowed_country {
    default no;                              
    US yes;
   }
Run Code Online (Sandbox Code Playgroud)

站点可用/默认中的相关服务器 {} 部分:

block countries
      if ($allowed_country = no) {
        return 444;
      }
Run Code Online (Sandbox Code Playgroud)

我们欢迎所有的建议!

更新:

$geoip_country_code 变量在 LAN IP 访问时返回“-”,但添加“- yes;” 到 geoip_county 块也不起作用。

小智 5

我找到了一个解决方案 - 不知道它是否“干净”。如果之前匹配了 lan IP 条件,只需覆盖 $allowed_country 变量。

geo $lan-ip {
默认没有;
192.168.1.0/24 是;
}

和“#block countries”部分之前的这个块

if ($lan-ip = yes) {
设置 $allowed_country yes;
}