Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I rate limit page requests by IP address

I am getting hit numerous times by crawlers on a page which triggers an API call. I would like to limit access to that page for bots who do not respect my robots.txt.

Note: This question is not a duplicate.. I want rate limiting not IP blacklisting.

like image 380
Abram Avatar asked Jan 13 '16 18:01

Abram


People also ask

Is rate limiting by IP?

Rate limiting runs within an application, rather than running on the web server itself. Typically, rate limiting is based on tracking the IP addresses that requests are coming from, and tracking how much time elapses between each request.

What is WAF rate-based rule?

A rate-based rule tracks the rate of requests for each originating IP address, and triggers the rule action on IPs with rates that go over a limit. You set the limit as the number of requests per 5-minute time span.


2 Answers

Check out the gem: Rack::Attack!

Battle-tested in production environments.

  • GiHub: https://github.com/kickstarter/rack-attack
  • Video from RailsConf: https://www.youtube.com/watch?v=m1UwxsZD6sw
like image 59
Tilo Avatar answered Sep 24 '22 00:09

Tilo


If you are using redis in your project you can very simply implement requests counter for API request. This approach allows you not to just limit robots access, but limit different API request using different policies based on your preferences. Take a loook on this gem or follow this guide if you want to implement limit by yourself.

like image 31
kimrgrey Avatar answered Sep 26 '22 00:09

kimrgrey