如何让 Rack::Attack 在负载均衡器后面工作?
How to make Rack::Attack work behind a load balancer?
我使用了 Rack::Attack 的示例节流代码。
throttle('req/ip', limit: 100, period: 5.minutes) do |req|
req.ip unless req.path.starts_with?('/assets')
end
这在我们的临时服务器上运行良好,但立即 运行 进入生产限制,因为 req.ip returns 我们的负载均衡器的 IP 地址而不是 remote_ip客户的。
请注意,remote_ip 是 ActionDispatch::Request 中的方法,但不是 Rack::Attack::Request。
我们在 Ruby 2.2.
上使用 Rails 3.2.2
我可以通过向 Rack::Attack::Request
添加一个方法来让它工作
class Rack::Attack
class Request < ::Rack::Request
def remote_ip
@remote_ip ||= (env['action_dispatch.remote_ip'] || ip).to_s
end
end
end
然后使用
req.remote_ip unless req.path.starts_with?('/assets')
我使用了 Rack::Attack 的示例节流代码。
throttle('req/ip', limit: 100, period: 5.minutes) do |req|
req.ip unless req.path.starts_with?('/assets')
end
这在我们的临时服务器上运行良好,但立即 运行 进入生产限制,因为 req.ip returns 我们的负载均衡器的 IP 地址而不是 remote_ip客户的。
请注意,remote_ip 是 ActionDispatch::Request 中的方法,但不是 Rack::Attack::Request。
我们在 Ruby 2.2.
上使用 Rails 3.2.2我可以通过向 Rack::Attack::Request
添加一个方法来让它工作class Rack::Attack
class Request < ::Rack::Request
def remote_ip
@remote_ip ||= (env['action_dispatch.remote_ip'] || ip).to_s
end
end
end
然后使用
req.remote_ip unless req.path.starts_with?('/assets')