在 Debian 11 上设置 GeoIP 与 Nginx 配合使用

在 Debian 11 上设置 GeoIP 与 Nginx 配合使用

我正在尝试设置 Nginx v. 1.24.0 和 GeoIPupdate,以便不来自特定国家/地区的用户无法访问 Web 服务器。

然而,当我遵循每个指南时,我发现它已经过时了,要么链接不起作用,因为 MaxMind 删除了它们,要么他们认为我有需要安装的模块。

甚至连官方的 Nginx wiki 也帮不上什么忙(https://nginx.org/en/docs/http/ngx_http_geoip_module.html

我已经束手无策了,不知道该怎么办。我只想知道如何让 GeoIPupdate 与 Nginx 一起工作,因为我已经运行了 GeoIPupdate。

我尝试过这个指南:https://github.com/leev/ngx_http_geoip2_module 但现在我得到了这个:

Apr 24 15:25:51 localhost nginx[175588]: nginx: [emerg] unknown directive "if($allowed_country" in /etc/nginx/sites-enabled/default:80
Apr 24 15:25:51 localhost nginx[175588]: nginx: configuration file /etc/nginx/nginx.conf test failed

有问题的配置(在服务器部分):

        if($allowed_country = no) {
                return 403;
        }

检查 Nginx 模块后,我确实看到 50-mod-http-geoip.conf 确实存在并且应该已经加载,但是写入 nginx -V 并不能输入该模块。

最有趣的部分是,nginx.conf 确实适用于允许的国家,因为我没有从中收到任何错误。

答案1

我用最搞笑和最悲伤的方式自己修复了它。

请记住,我已经研究这个奇怪的错误几天了,我发现这是因为我添加了一个选项卡,所以我的 OCD 可以在配置文件中感到高兴。

我复制并粘贴了这个并且成功了。 https://gist.github.com/dunderrrrrr/8d3fced1f73de2d70ede38f39c88d215

相关内容