ruby-on-railsherokugetaddrinfogeokit

Rails Heroku SocketError: getaddrinfo: Name or service not known


I am on a heroku app, Ruby 2.2.0, Rails 4.0.12 and using the geokit-rails gem.

When I do a IpGeocoder.geocode(request.remote_ip) I get the error:

Caught an error during Ip geocoding call: getaddrinfo: Name or service not known

I searched online and found that the solution is to make an initialiser and require a library:

require 'resolv-replace'

When I do so I get the error:

Caught an error during Ip geocoding call: Hostname not known: api.hostip.info

I am not sure what I should do then. On my development machine works fine. Any clue what the issue is?


Solution

  • Heroku uses forwarding, that causes request.remote_ip return internal heroku ip address, which cannot be processed by IpGeocoder for some reason.

    Try to use request.env['HTTP_X_FORWARDED_FOR'] insted.