You do this by telling iptables to look at the X-Forwarded-For header when deciding what IP that request is coming from.
This blog posts explains the whole thing: https://centos.tips/fail2ban-behind-a-proxyload-balancer/
I have no idea if using .htaccess rules would be better than this solution, I just know that this one works.