Block user-agents and referrers with Nginx map

Posted on March 18, 2015 at 8:09 pm

The code below must be included inside the http{} block:

## Add here all user agents that are to be blocked.
map $http_user_agent $bad_bot {
    default 0;
    libwww-perl                      1;
    ~(?i)(httrack|htmlparser|libwww) 1;
}
 
## Add here all referrers that are to blocked.
map $http_referer $bad_referer {
    default 0;
    ~(?i)(babes|click|diamond|forsale|girl|jewelry|love|nudit|organic|poker|porn|poweroversoftware|sex|teen|webcam|zippo|casino|replica) 1;
}

Then inside the server{} block add this:

    ## Deny access based on the User-Agent header.
    if ($bad_bot) {
        return 444;
    }
    ## Deny access based on the Referer header.
    if ($bad_referer) {
        return 444;
    }

Reference:

perusio/wordpress-nginx/blacklist.conf
perusio/wordpress-nginx/sites-available/example.com.conf

Updated on March 19, 2015 at 10:53 am

Receive updates via email

Other Posts

Updated Posts