How to block bad bots User-Agents in Nginx

To block bots by User-Agent in Nginx, add the following to the server entry of the website.

if ($http_user_agent ~ (semrush|opensiteexplorer|ahrefs|seekport|mj12bot)) {
    return 403;
}

Restart Nginx

systemctl restart nginx

To block acces to a specific file or folder, use

location /file-to-block.php {
    return 408;
}

If you are using Apache web server, see How to block Bad Bots (User Agents) using .htaccess or Block User-Agent using Cloudflare

Back to Nginx

Need help with Linux Server or WordPress? We can help!

Leave a Reply

Your email address will not be published. Required fields are marked *