【拒绝恶意蜘蛛爬虫】
/www/server/panel/vhost/nginx下找到如wenxian8.com.conf
(也可以:在宝塔-网站-设置-设置文件内) 加上下面代码防恶意蜘蛛爬虫,
注意:在server { 内, 且在location之前,
# 拒绝常见恶意爬虫、采集器、扫描器
if ($http_user_agent ~* "(amazonbot|ClaudeBot|AhrefsBot|SemrushBot|DotBot|MJ12bot|PetalBot|YandexBot|spbot|Bytespider|ClaudeBot|Exabot|MegaIndex|SeznamBot|Trendictionbot|ZoominfoBot|python-requests|Go-http-client|curl|wget|httpclient|libwww-perl|Java|DataForSeoBot|Applebot|dataforseo-bot|gptbot)") {
return 403;
}
注意千万别拦截:AppleWebKit ,一栏就全部打不开,4个小时的教训
【Nginx端 拒绝大量高页码访问】
如 /page/582、/page/685
很可能是爬虫在试图穷举所有分页地址,属于“伪静态深度爬虫”行为,这种行为会给数据库和服务器带来极大负载。
建议的防护措施:
一、限制分页深度,防止无限翻页导致数据库崩溃。
Nginx 限制分页最大页数(如超过 30 页直接返回 404)适用于 Nginx 伪静态规则(推荐)
在你每个站点的 Nginx 配置文件(如 /www/server/panel/vhost/nginx/shu29.com.conf)中,server{} 块内添加:
(这个很有可能至伪静态失效)
# 只限制 /page/xxx 分页路径(排除 /tag/... 或 /category/...)
if ($request_uri ~ "^/page/([3-9][0-9]|[1-9][0-9]{2,})/?$") {
return 404;
}
# 禁止列目录
autoindex off;
# 阻止执行 uploads 里的 php(强烈建议)
location ~* ^/wp-content/uploads/.*\.php$ {
deny all;
}
二、在wordpress内的functions.php加入
// 限制前台分页最大为 30 页(首页、归档、搜索)
add_action('template_redirect', function () {
if (is_admin()) return;
$max_page = 30;
$paged = (int) get_query_var('paged');
if ((is_home() || is_archive() || is_search()) && $paged > $max_page) {
wp_redirect(home_url());
exit;
}
});
// 阻止大分页查询执行,节省数据库资源
add_action('pre_get_posts', function($query) {
if (!is_admin() && $query->is_main_query()) {
$paged = (int) get_query_var('paged');
if ($paged > 30) {
// 可替换为 404 页面
wp_redirect(home_url());
exit;
}
}
});