Skip to content

Instantly share code, notes, and snippets.

@fengqi
Last active June 24, 2022 03:34
Show Gist options
  • Save fengqi/18050b2926fe05cdd5597a4ad6ac8b74 to your computer and use it in GitHub Desktop.
Save fengqi/18050b2926fe05cdd5597a4ad6ac8b74 to your computer and use it in GitHub Desktop.
nginx自动屏蔽扫描的ip
#!/bin/bash
line=1000
times=10
conf=/opt/nginx/conf/blockip.conf
tail /data/logs/nginx/access.log -n $line | \
grep -E '("status":"404"|"status":"302")' | jq -r '.remote_addr' | \
sort | uniq -c | \
awk '$1>$times{print "deny "$2 ";"}' >> $conf
deny=$(sort $conf | uniq -c | awk '{print "deny "$3}')
echo $deny | sed "s/; /;\n/g" > $conf
/usr/local/sbin/nginx -t || exit
/usr/bin/systemctl reload nginx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment