
Rate Limiting Scrapers on Cloudflare Without Blocking Google
So after that strange spike in Search Console we opened Cloudflare to see do we have bots on the blog also, and yes, three IP addresses made 657, 651 and 335 requests during last 24 hours, for a blog like ours it is a lot. With the spike Cloudflare can't help us, that one lives on the Google side, but these three guys were really knocking.
We are on the free plan, there you have one rate limiting rule and ours was taken already, so we were opening this rule and adding the blog into it with an or. Now it says 10 page requests in 10 seconds from one IP, more than this and you are blocked for 10 seconds. We took out from counting the images, css, js and fonts folders, if not then our own visitor gets blocked just by opening one page.
(http.host eq "example.com"
and not cf.client.bot
and not starts_with(http.request.uri.path, "/css/")
and not starts_with(http.request.uri.path, "/js/")
and not starts_with(http.request.uri.path, "/images/")
and not starts_with(http.request.uri.path, "/fonts/")
and not starts_with(http.request.uri.path, "/cdn-cgi/"))
Next thing, we got scared a bit, because 10 sounds small, and imagine Google or Bing is coming to index the blog and our own rule is throwing them out, then the cure is worse than the sickness, so in the expression we have cf.client.bot, this one is true for crawlers which Cloudflare has verified, and we put not in front of it.
But everyone can put Googlebot into the user agent, no? So we did put it, into curl, and started hitting the blog as a fake Google, and on request number 11 we got our 429, good, so the name is not enough for them.
Our first test was bad, we have to tell. We were sending 25 requests all together and got 25 times OK, and we were sitting and thinking what we did wrong in the expression. Nothing was wrong, the counter just doesn't have time to count them. One after another it works, somewhere after 10 or 12 pages 429 starts, we waited like 14 seconds and the page was back, and images we could ask as many as we want.
for i in $(seq 1 40); do
curl -s -o /dev/null -w "%{http_code} " https://example.com/blog/
done
Then we went back to Search Console and did the live test on one page, you know, to be blocked from Google by our own rule would be so stupid, but it said the page is fine. About Bing we have nothing to say because we didn't check it, maybe one day.
There is also the Bot Fight Mode switch and we didn't touch it, it is for the whole domain at once and we had a subdomain where some scripts are calling, and a script can't click on a challenge, it will just fail and we will learn about it from somebody angry.
And about this 657, when we divide it on 24 hours and then on 60 it is not even one per minute, so this rule would be sleeping if they come like this the whole day, it wakes up only when somebody is pulling many pages in a few seconds. We are leaving it anyway since we pay zero for it. Limit per user is another thing, Cloudflare on free is not knowing who is logged in on our app, there we do it in Rails, and for the contact form Turnstile is doing the job.