diff --git a/lib/geocoder/request.rb b/lib/geocoder/request.rb
index 5f71d46bc86455f9be4b2771b545a16bf5384653..3e1dbc823a7d7d769a289f5dbed9adeadb982e07 100644
--- a/lib/geocoder/request.rb
+++ b/lib/geocoder/request.rb
@@ -5,7 +5,13 @@ module Geocoder
 
     def location
       unless defined?(@location)
-        @location = Geocoder.search(ip).first
+        if env.has_key?('HTTP_X_REAL_IP')
+          @location = Geocoder.search(env['HTTP_X_REAL_IP']).first
+        elsif env.has_key?('HTTP_X_FORWARDED_FOR')
+          @location = Geocoder.search(env['HTTP_X_FORWARDED_FOR']).first
+        else
+          @location = Geocoder.search(ip).first
+        end
       end
       @location
     end