基于 IP 的地理位置查询¶
模块:mod_maxminddb
要求¶
软件包:GeoIP2 C API 和库 libmaxminddb
https://github.com/maxmind/libmaxminddb
https://dev.maxmind.com/geoip/
https://dev.maxmind.com/geoip/geoip2/geolite2/
概览¶
mod_maxminddb 是一个用于快速 IP/位置查询的模块,从 lighttpd 1.4.54 开始可用。它使用 MaxMind GeoIP2 数据库。如果在数据库中找到了 IP,该模块会将相应的环境变量设置到请求中,从而使这些信息可供其他模块/fcgi 使用。
https://dev.maxmind.com/geoip/geoip2/geolite2/#Downloads
mod_maxminddb 使用 MaxMind GeoIP2 数据库。这是 GeoIP Legacy 数据库的后续版本。GeoIP Legacy 数据库已于 2019 年 1 月 2 日停用 (https://dev.maxmind.com/geoip/legacy/release-notes/),mod_geoip 现已弃用。
注意:lighttpd 1.4.70 之前的 mod_maxminddb 期望每个连接只有一个客户端,并按连接缓存 geoip 数据库查询;lighttpd 1.4.70 之前的 mod_maxminddb 不期望在同一连接上的请求之间 IP 发生变化。在 lighttpd 1.4.70 及更高版本中,mod_maxminddb 可以与 mod_extforward 结合使用,以处理配置为重用连接以服务多个客户端的负载均衡器发出的连接。在 lighttpd 1.4.70 之前,mod_maxminddb 应与配置为安全(仅为同一客户端重用连接,不为不同客户端重用)的负载均衡器结合使用。或者,代替 mod_extforward 和 mod_maxminddb,可以使用 mod_magnet 和 Lua 脚本来解析 X-Forwarded-For
或其他请求头,然后查询 maxminddb。
配置选项¶
maxminddb.activate = "enable" (default: "disable")
maxminddb.db = "/path/to/the/GeoIP2/database.mmdb"
maxminddb.env = (...) - list of environment variables to set and associated lookup keys in GeoIP2 .mmdb.
maxminddb.env = (
"GEOIP_COUNTRY_CODE" => "country/iso_code",
"GEOIP_COUNTRY_NAME" => "country/names/en",
"GEOIP_CITY_NAME" => "city/names/en",
"GEOIP_CITY_LATITUDE" => "location/latitude",
"GEOIP_CITY_LONGITUDE" => "location/longitude",
)
基于 GeoIP2 数据重定向可以在 lighttpd 内部使用 mod_magnet 或使用后端 CGI、FastCGI、SCGI 等来实现。