Answers:
据我所知,Auth Basic模块不支持此功能,但是您可以使用Fail2ban来做到这一点。
使用不存在的用户进行测试,您将在错误日志中看到以下内容:
2012/08/25 10:07:01 [error] 5866#0: *1 no user/password was provided for basic authentication, client: 127.0.0.1, server: localhost, request: "GET /pma HTTP/1.1", host: "localhost:81"
2012/08/25 10:07:04 [error] 5866#0: *1 user "ajfkla" was not found in "/etc/nginx/htpasswd", client: 127.0.0.1, server: localhost, request: "GET /pma HTTP/1.1", host: "localhost:81"
然后创建必要的过滤器:
/etc/fail2ban/filter.d/nginx-auth.conf
[Definition]
failregex = no user/password was provided for basic authentication.*client: <HOST>
user .* was not found in.*client: <HOST>
user .* password mismatch.*client: <HOST>
ignoreregex = </host></host></host>
/etc/fail2ban/jail.conf
[nginx-auth]
enabled = true
filter = nginx-auth
action = iptables[name=NoAuthFailures, port=80, protocol=tcp]
logpath = /var/log/nginx*/*error*.log
bantime = 3600 # 1 hour
maxretry = 3
测试Fail2Ban规则:
fail2ban-regex /var/log/nginx/localhost.error_log /etc/fail2ban/filter.d/nginx-auth.conf
Failregex
|- Regular expressions:
| [1] no user/password was provided for basic authentication.*client: <HOST>
| [2] user .* was not found in.*client: <HOST>
| [3] user .* password mismatch.*client: <HOST>
|
`- Number of matches:
[1] 1 match(es)
[2] 2 match(es)
[3] 0 match(es)
Ignoreregex
|- Regular expressions:
|
`- Number of matches:
Summary
=======
Addresses found:
[1]
127.0.0.1 (Sat Aug 25 10:07:01 2012)
[2]
127.0.0.1 (Sat Aug 25 10:07:04 2012)
127.0.0.1 (Sat Aug 25 10:07:07 2012)
[3]
PS:由于Fail2ban获取要禁止的日志文件,因此请确保logpath
与您的配置匹配。
我很惊讶,没有其他人提供这种解决方案/解决方法。
Nginx basic-auth并htpasswd
支持带有可选成本变量的bcrypt密码加密。Bcrypt的设计速度很慢,因此对尝试不同密码的速度提供了严格的限制。
在创建基本身份验证用户名/密码时使用
htpasswd -B -C 12 path/to/users.db <username>
以12的成本,您的服务器将可能无法每秒尝试几次多次密码,将密码增加到14,则每次密码尝试大约可能需要1s。
使用该配置后,即使攻击者连续多年尝试输入密码,任何合理的密码也不会受到暴力攻击。
例如,对8个字符的字母数字密码进行每秒10次密码尝试的蛮力攻击将花费692,351年62**8 / (10*3600*24*365)
。
与设置“智能”请求限制相比,这更容易配置并且更加简单。
Nginx-HTTP-Auth-Digest模块可以将基本auth模块替换为许多其他功能,例如重试和超时。此处提供其他文档
唯一的缺点是,这可能需要重建nginx
bcrypt
在Nginx的基本身份验证中使用ed密码,这将很有用,但显然不能。