I don't know how heroku works, but you might be behind a load balancer. You'll need to set your TRUSTED_PROXIES
to get request.remote_ip to be the HTTP_X_FORWARDED_FOR
address.
You can check to see if this is your problem by adding an action to one of your controllers like this:
def remote_ip
render :text => "REMOTE_ADDR: %s<br/>remote_ip: %s<br/>HTTP_X_FORWARDED_FOR: %s" %
[ request.env['REMOTE_ADDR'],
request.remote_ip,
request.env['HTTP_X_FORWARDED_FOR'] ]
end
If you've got an HTTP_X_FORWARDED_FOR
, then you need to tell Rails about trusted proxies. Once you do that, your request.remote_ip
and your HTTP_X_FORWARDED_FOR
ips will be the same.
In your production.rb
, add these lines, where the allowed_ips regex includes your load balancer IPs. Replace the a.b.c.
with the load balancer IPs you get from heroku.
# Setup Trusted Proxies
allowed_ips = /^a\.b\.c\.|^127\.0\.0\.1$|^(10|172\.(1[6-9]|2[0-9]|30|31)|192\.168)\./i
ActionController::Request.const_set("TRUSTED_PROXIES", allowed_ips)