How can you block or filter IP addresses on Heroku?

Matt Smith picture Matt Smith · Aug 27, 2012 · Viewed 11.8k times · Source

Is there a way to implement IP filtering or IP access rules much like I would with nginx/apache to restrict or block certain IPs on Heroku?

Note: I know this can be done from within my application (Rails 3.2) very easily but I don't think this is the most efficient use of my resources on Heroku. Also, a Rack based solution would be better than implementing the filtering in Rails.

Answer

Phil picture Phil · Oct 10, 2014

You should check out rack-attack. Looks like it does the same as rack-block, but is much more widely used and updated frequently. To block a specific IP you can do this:

# Block requests from 1.2.3.4
Rack::Attack.blacklist('block 1.2.3.4') do |req|
  # Requests are blocked if the return value is truthy
  '1.2.3.4' == req.ip
end