### [网站被人恶意刷流量和攻击,Nginx限流和防火墙规则挡一下](https://www.jiyueip.com/article/14200) **Published:** 2026-08-07T03:25:14 **Author:** 斑斓 **Excerpt:** 网站被刷流量和攻击怎么挡。Nginx limit_req限流、fail2ban封IP、UA和Referer过滤… 小站有时候会被莫名其妙的大量请求刷爆。可能是爬虫、可能是有人扫漏洞。Nginx 自带 limit\_req 和 fail2ban 配合能挡住大部分。 服务器用的雨云,地址 [rain.jiyueip.com](https://rain.jiyueip.com),优惠码 **admin01** 五折。 ## Nginx limit\_req 限流 ``` http { limit_req_zone $binary_remote_addr zone=mylimit:10m rate=10r/s; server { location / { limit_req zone=mylimit burst=20 nodelay; } } } ``` rate=10r/s 每秒最多 10 个请求。burst=20 短时间内超额可以排队 20 个。超额返回 503。正常用户感觉不到,刷子直接被拦截。 ## fail2ban 封恶意 IP 装好之后在 /etc/fail2ban/jail.local 里加: ``` [nginx-http-auth] enabled = true maxretry = 5 bantime = 3600 [nginx-botsearch] enabled = true maxretry = 3 bantime = 3600 ``` 多次触发规则的 IP 自动封禁一段时间。 ## UA 和 Referer 过滤 一些爬虫靠 User-Agent 可以识别。Nginx 里直接拒绝常见的扫描工具 UA 和异常的 Referer。还有防止盗链保护图片资源被别的网站直接引用消耗你的带宽。 Cloudflare 免费 CDN 自带 DDoS 防护和 WAF 防火墙。套一层 Cloudflare 之后服务器 IP 不暴露,攻击全打在 CDN 上。 这几样配好之后被刷的概率低很多。雨云 rain.jiyueip.com,优惠码 admin01 五折。 **Tags:** Linux服务器, Ubuntu服务器 **Categories:** 行业洞察 ---