How to block UserAgent with nginx

Open host file:

sudo mcedit /etc/nginx/sites-available/...

And put into server section condition:

if ($http_user_agent ~* (BadUserAgent) ) {
    return 404;
}

Where “BadUserAgent” change to what you want.

Do you want to block multiple UserAgents? Use this:

if ($http_user_agent ~* (BadUserAgentFirst|BadUserAgentSecond|BadUserAgentNext) ) {
    return 404;
}

At last, check nginx configuration and reload it if it’s ok:

sudo nginx -t
sudo service nginx reload
 17   1 mon  
Popular