网站配置Nginx来抵御CC攻击

9 min read
#禁止非 Mozilla/ 请求头的访问
if ($http_user_agent !~* "Mozilla/") {
    return 403;
}
#禁止非GET|HEAD|POST方式的抓取
if ($request_method !~ ^(GET|HEAD|POST)$) {
    return 403;
}
#禁止Scrapy等爬虫工具的采集
if ($http_user_agent ~* (Scrapy|Curl|HttpClient)) {
    return 403;
}
#限制http版本号
if ($server_protocol !~* "HTTP/2.0|HTTP/3.0|SPDY/3.1") {
    return 403;
}

把上面文件保存为 agent_deny.conf ,上传到 Nginx 的 conf.d 文件夹,然后在要生效的网站 server 模块中引入这个配置文件,并 Reload 重载 Nginx 即可生效:

include /etc/nginx/conf.d/*.conf;