大网数据广告活动
服务器运维问答

Mitigating 404 bomb with Nginx

· 52CE 我爱测

I am hit with 404 queries and this is bringing down my machine. Close to all of my pages are http cached and I have some basic Dos protection with limit_conn_zone $http_x_forwarded_for zone=addr:10m; limit_conn addr 8; limit_req_…

问题原文

原帖链接 · Quintin Par · CC BY-SA 4.0

I am hit with 404 queries and this is bringing down my machine. Close to all of my pages are http cached and I have some basic Dos protection with

limit_conn_zone $http_x_forwarded_for zone=addr:10m;
limit_conn addr 8;

limit_req_zone $http_x_forwarded_for zone=one:10m rate=2r/s;
limit_req zone=one burst=50;

client_body_timeout 5s;
client_header_timeout 5s;
send_timeout 10s;

What can I do to prevent these 404 attacks besides the above?

已采纳解答

原帖链接 · Leo Gallego · CC BY-SA 4.0

After setting limit_req and limit_conn in nginx did you enable them in the virtualhost? As in:

server {
    # ...
    location / {
        limit_req zone=one;
        limit_conn addr 10;

    # ...
    }
}

Also, fail2ban is a log parser for auto creating dynamic rules in the firewall (iptables). You can create a filter and action in fail2ban which filters the origin IP of the 404 and blocks them after a number of attempts, or you can filter the limit_req and limit_conn logs so you can ban those IPs instead (blocking the 404 clients IP might cause some unwanted blocks).

vim /etc/fail2ban/jail.d/nginx.conf

Copy this:

[nginx-req-limit]

enabled = true
filter = nginx-req-limit
action = iptables-multiport[name=ReqLimit, port="http,https", protocol=tcp]
logpath = /var/log/nginx/*error.log
findtime = 600
bantime = 7200
maxretry = 10
  • findtime is the time in seconds for the occurrences in maxretry, in this case it would trigger after 10 events in 10 minutes (600 seconds).
  • bantime is the time to blacklist the IP in the firewall. Also in seconds. In this case it would block the offending IP for 2 hours (7200 seconds).
  • logpathis the error log you configured for your virtualhost in nginx.

Make sure that jail.conf includes the jail.d/*.conf reference and restart the service:

service fail2ban restart

This should help you avoid DDoS attacks.

Another option worth considering is using a CDN as stated in the comment above. Cloudflare has a nice free version which can help a lot, it has a Web Application Firewall that blocks some of the bad bots and stuff. The pro/business versions have more options, but cost money.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-05-07)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。