如何在Heroku上阻止或过滤IP地址?

时间:2012-08-27 04:34:32

标签: ruby-on-rails security heroku ip rack

有没有办法实现IP过滤或IP访问规则,就像使用nginx / apache限制或阻止Heroku上的某些IP一样?

注意:我知道这可以在我的应用程序(Rails 3.2)中轻松完成,但我不认为这是Heroku上最有效的资源使用。此外,基于Rack的解决方案比在Rails中实现过滤更好。

2 个答案:

答案 0 :(得分:16)

您应该查看rack-attack。看起来它与rack-block相同,但是更频繁地使用和更新。要阻止特定IP,您可以执行以下操作:

# 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

答案 1 :(得分:8)

我添加了'rack-block'作为Rack中间件。在config / initializers中,添加一个新文件:

YourApp::Application.configure do

  config.middleware.insert_before(Rack::Lock, Rack::Block) do  
    # Add your rules with the rack-block syntax in here
  end

end

像魅力一样。