Nginx配置geoip_country时报unknown directive错误如何解决?
问题核心原因
- 你加载的模块是
ngx_http_geoip2_module.so(MaxMind GeoIP2 版本第三方模块),但配置中使用的geoip_country是旧版GeoIP1(Legacy版本)官方模块的专属指令,两个版本模块的配置指令完全不兼容 - Nginx编译参数中的
--with-http_geoip_module=dynamic对应的是Nginx官方自带的GeoIP1模块,和你加载的GeoIP2模块无关联
修复方案(二选一即可)
方案1:继续使用GeoIP2模块(更推荐,MaxMind已停止维护GeoIP1的数据库更新)
- 保留当前的
load_module /etc/nginx/modules-enabled/ngx_http_geoip2_module.so;配置 - 将原
geoip_country相关配置替换为GeoIP2的对应语法,示例如下:
# 需提前将下载的GeoLite2-Country.mmdb数据库放到对应路径 geoip2 /usr/share/GeoIP/GeoLite2-Country.mmdb { auto_reload 5m; $geoip2_data_country_code country iso_code; $geoip2_data_country_name country names zh-CN; }
- 执行
nginx -t验证配置语法,验证通过后执行systemctl reload nginx即可生效
注意:使用该方案需提前安装libmaxminddb依赖库,GeoIP2模块仅支持mmdb格式的数据库,不兼容GeoIP1的dat格式数据库
方案2:切换使用官方自带的GeoIP1模块
- 删除现有加载GeoIP2模块的配置行:
load_module /etc/nginx/modules-enabled/ngx_http_geoip2_module.so; - 新增加载官方GeoIP1动态模块的配置:
load_module modules/ngx_http_geoip_module.so;
- 原有
geoip_country相关配置无需修改,执行nginx -t验证通过后重载Nginx即可
内容的提问来源于stack exchange,提问作者Michael Nielsen
相关产品推荐
相关产品推荐

