Avishai
Avishai

Reputation: 4752

Using GeoIP with Nginx/Passenger in a Rails app

I've been trying to set up my Rails app (deployed on Passenger 3 + Nginx) to work with MaxMind's GeoLite city, to no avail.

# 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;

Then in my Rails app I do something like this:

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"]
}

But it always returns nil for all values. Am I setting the variables correctly? Could the geo database be configured incorrectly? Nginx doesn't throw any errors and the --with-http_geoip_module compile flag is set.

Upvotes: 1

Views: 742

Answers (1)

Avishai
Avishai

Reputation: 4752

Figured this out. It turns out that in addition to specifying geoip_city in your http {} block, you also have to specify the passenger_set_cgi_param directives in your server {} block, and prefix your variables with HTTP_ so that they get passed.

Upvotes: 1

Related Questions