Nginx配置屏蔽PC端放行爬虫代码
苹果CMSV10作为当今影视站的主要CMS,也是受到一些坏人的关注和攻击,特别的搜索引擎靠前的网站,深受困扰,为了不让PC端显示,我们可以配置nginx屏蔽PC端,放行爬虫代码。
分类:
Nginx配置屏蔽PC端放行爬虫代码
# === 屏蔽PC端(修复版)===
set $allow_request 1;
# --- 新增:放行主流搜索引擎爬虫(写在最前面)---
# Google 爬虫(包括PC和移动版)
if ($http_user_agent ~* "Googlebot") {
set $allow_request 1;
}
# 百度爬虫
if ($http_user_agent ~* "Baiduspider") {
set $allow_request 1;
}
# 必应爬虫
if ($http_user_agent ~* "bingbot") {
set $allow_request 1;
}
# 360爬虫
if ($http_user_agent ~* "360Spider") {
set $allow_request 1;
}
# 搜狗爬虫
if ($http_user_agent ~* "Sogou") {
set $allow_request 1;
}
# 神马爬虫
if ($http_user_agent ~* "YisouSpider") {
set $allow_request 1;
}
# 头条爬虫
if ($http_user_agent ~* "Bytespider") {
set $allow_request 1;
}
# --- 爬虫白名单结束 ---
# 移动设备检测
if ($http_user_agent ~* "(Mobile|Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|webOS)") {
set $allow_request 1;
}
if ($http_user_agent !~* "(Mobile|Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|webOS|Version/\d+\.\d+.*Safari/)") {
set $allow_request 0;
}
# 排除SSL验证路径
if ($uri ~ ^/\.well-known/acme-challenge/) {
set $allow_request 1;
}
# 如果不是移动设备且不在排除路径中,返回404
if ($allow_request = 0) {
return 444;
}
将以上代码添加到nginx的配置文件中,然后重启nginx即可。444可以改成404或是403,根据你的需求来。