Skip to content

Instantly share code, notes, and snippets.

@dergachev
Last active December 18, 2015 09:19
Show Gist options
  • Save dergachev/5760625 to your computer and use it in GitHub Desktop.
Save dergachev/5760625 to your computer and use it in GitHub Desktop.
# prevents error: could not build the map_hash, you should increase map_hash_bucket_size: 64
map_hash_bucket_size 192;

map $http_user_agent $bad_bot {
        default 0;
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36" 1;
}

# must be in http context
geo $abuse {
  default 0;
  # fake bad IPs (local network), for test purposes
  192.168.0.0/16 1;
  # real bad IPs: over-aggressive crawlers from China
  119.98.0.0/16 1;
  119.96.0.0/16 1;
}

server {

# because nginx doesn't support nested if statemetns; adapted from http://rosslawley.co.uk/2010/01/nginx-how-to-multiple-if-statements/
if ($abuse = 0) {
  set $bad_bot 0;
}
if ($bad_bot = 1) {
 return 403;
}

# poor man's debugging
# rewrite ^ http://httpbin.org/get?debug=$abuse last; break;
@dergachev
Copy link
Author

This gist has been merged into https://github.com/dergachev/nginx-examples

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment