Loyalsoldier / geoip

🌚 🌍 🌝 GeoIP 规则文件加强版,支持自行定制 V2Ray dat 格式文件 geoip.dat、MaxMind mmdb 格式文件、sing-box SRS 格式文件、mihomo MRS 格式文件、Clash ruleset、Surge ruleset 等。Enhanced edition of GeoIP files for V2Ray, Xray-core, sing-box, Clash, mihomo, Shadowrocket, Quantumult X, Surge, hysteria, Trojan-Go, Leaf, etc.
Creative Commons Attribution Share Alike 4.0 International
3.61k stars 620 forks source link

请问nginx能用吗 #180

Closed richzhu369 closed 1 month ago

richzhu369 commented 1 month ago

请问nginx能用吗,我想用来判断国家

richzhu369 commented 1 month ago
http {
    geoip_country /path/to/GeoIP/GeoIP.dat;

    server {
        listen 80;
        server_name your_domain.com;

        location / {
            # 检查访问者国家,只允许中国(CN)访问和特定 IP 地址(1.1.1.1)
            if ($geoip_country_code != CN) {
                if ($remote_addr != 18.163.177.7) {
                    return 403;
                }
            }

            # 其他配置
        }
    }
}
Loyalsoldier commented 1 month ago

本项目中的 dat 格式 geoip 文件是 V2Ray 专属的,不能用于 Nginx。

Nginx 中的 dat 格式 geoip 数据库来源于 MaxMind,但这个格式已经被 MaxMind 列为过时,MaxMind 现在推荐使用 mmdb 格式。本项目中的 mmdb 格式文件理论上可用于 Nginx,但我没有尝试过,代码中只保留了一个变量 iso_code(两位字母的国家代号) 。

在 Nginx 中使用 mmdb 格式 geoip 文件,可以参考:https://github.com/leev/ngx_http_geoip2_module

另外,对于 IP / CIDR 的简单访问控制,也可以使用 Nginx 的 allowdeny,参考:https://github.com/Loyalsoldier/geoip/tree/release/nginx