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

Advertisement

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

Add a comment

Leave a Reply

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

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Advertisement