For that, the best solution is, as you said, IPtables at the 3 different machines.
Actually, that is not a good solution as the remote ip ($remote_addr
in Nginx) will be from Amazon's loadbalancer. Banning that will result in all traffic forwarded getting banned.
You'll have to inspect the packets and find the HTTP X-Forwarded-For
header, IPtables isn't protocol aware like that.
I settled for the following solution to 2 naughty IPs in Nginx
set $client_ip $remote_addr;
if ($http_x_forwarded_for) {
set $client_ip $http_x_forwarded_for;
}
if ($client_ip = "123.123.123.123") {
return 403;
}
if ($client_ip = "123.123.123.234") {
return 403;
}
Introducing a variable $client_ip
, just so that I could also test this locally, where there is no http_x_forwarded_for
available..
Slightly offtopic but posting for convenience, I also added that client ip to my access logs:
log_format main "\$client_ip - \$remote_user [\$time_local] \"\$request\" \$status \$body_bytes_sent \"\$http_referer\" \"\$http_user_agent\"";
access_log /var/log/nginx.access.log main;
It's not pretty, but hope it helps