我一直在尝试设置我的 Rails 应用程序(部署在乘客 3 + Nginx 上)与 MaxMind 的 GeoLite 城市一起使用,但无济于事。
# nginx.conf
# IP Geolocation - City Level
geoip_city /usr/share/GeoIP/GeoLiteCity.dat;
passenger_set_cgi_param HTTP_GEOIP_CITY_COUNTRY_CODE $geoip_city_country_code;
passenger_set_cgi_param HTTP_GEOIP_CITY_COUNTRY_CODE3 $geoip_city_country_code3;
passenger_set_cgi_param HTTP_GEOIP_CITY_COUNTRY_NAME $geoip_city_country_name;
passenger_set_cgi_param HTTP_GEOIP_REGION $geoip_region;
passenger_set_cgi_param HTTP_GEOIP_CITY $geoip_city;
passenger_set_cgi_param HTTP_GEOIP_POSTAL_CODE $geoip_postal_code;
passenger_set_cgi_param HTTP_GEOIP_CITY_CONTINENT_CODE $geoip_city_continent_code;
passenger_set_cgi_param HTTP_GEOIP_LATITUDE $geoip_latitude;
passenger_set_cgi_param HTTP_GEOIP_LONGITUDE $geoip_longitude;
然后在我的 Rails 应用程序中,我执行以下操作:
geodata = {
country_code: request.env["HTTP_GEOIP_CITY_COUNTRY_CODE"],
country_name: request.env["HTTP_GEOIP_CITY_COUNTRY_NAME"],
region: request.env["HTTP_GEOIP_REGION"],
city_name: request.env["HTTP_GEOIP_CITY"],
zipcode: request.env["HTTP_GEOIP_POSTAL_CODE"],
lat: request.env["HTTP_GEOIP_LATITUDE"],
lng: request.env["HTTP_GEOIP_LONGITUDE"]
}
但它总是为所有值返回 nil。我是否正确设置了变量?地理数据库是否配置不正确?Nginx 不会抛出任何错误并且--with-http_geoip_module
设置了编译标志。