Is there a way to block an IP address without taking hits?

If some user uses a script/bot to spam my site with requests, what should I do to block them?

I can make a list of blocked IP addresses on my server, and each time someone visits a page, the server checks to see if they’re on the block list. The problem is that spam requests still count as hits because the server has to take the hit first and then block them.

How do I make my site completely ignore some IP addresses, so spammers cannot access my site at all?

Unfortunately, there is no way to block an IP address without hitting a server. The only thing you can hope for is that the spammer will see that the requests are not getting through and stop hammering your website. But the IP address needs to hit your website first before it can be blocked.

If you have your own domain name, you could also choose to use Cloudflare on your domain. That way, the request can be stopped by Cloudflare and doesn’t hit your hosting account.