server {} 里加入以下内容:

if ($http_user_agent ~ "Mozilla/4.0$|Mozilla/5.0$|Java|ApacheBench|Python-urllib|^$") {
        return 403;
}


注:有支付宝和易宝支付,需要将Mozilla/4.0和Java 删除


curl 模拟UserAgent访问

# curl http://www.test.com/index.html -A "ApacheBench"
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<h1>403 Forbidden</h1>
<p>You don't have permission to access the URL on this server.</body>
</html>


返回HTTP CODE 403