什么是最好的方式来阻止在Nginx的URL?

例如,我们正在为一些url获取exception多的stream量,例如:

http://www.example.com/search.php?type=0&mode=search&searchterm=Little

http://www.example.com/product.php?productid=12345678

nginx中阻止这些URL的最好方法是什么?

任何其他build议来限制特定页面每秒/分钟的连接数量?

如果传入stream量确实不寻常,并且您有信心说stream量是垃圾邮件,请考虑在防火墙中阻止该stream量。 堆栈中可以拒绝stream量的越低越好。 例如iptables -A INPUT -s 1.1.1.1 -j DROP 。 当然,用相关的IP地址replace1.1.1.1

一个更广泛的工具来保护您的服务器免受可疑的传入连接将fail2ban 。 你也可以制定一些特定于Nginx的规则。 Fail2ban保护您免受许多不同的攻击,不仅仅是解决您的具体问题。

对于特定于Nginx的解决scheme,它提供了一个名为ngx_http_access_module的模块,允许您根据IP地址允许或拒绝访问。 您将打开nginx.conf并添加include blockips.conf; 然后在你的Nginxconfiguration文件所在的位置创buildblockips.conf。 可能是/usr/local/nginx/conf/ 。 最后,根据需要添加拒绝configuration:

 deny 1.1.1.1; deny 2.2.2.0/24; deny 3.3.0.0/16; 

使用相同的原则, http://www.sonassi.com/knowledge-base/maintenance-page-with-nginx-with-specific-permitted-access/和http://wiki.nginx.org/HttpLimitReqModule

下面的代码将允许您禁止访问这些URL的特定IP,并且限制任何其他尝试访问这些URL的人。

 http { ... limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; ... server { .. set $deny_access off; if ($remote_addr ~ (bad.ip.add.ress|second.bad.ipadd.ress|third.bad.ipadd.ress)) { set $deny_access on; } if ($uri ~ "^/(search\.php|product\.php)\?(type=[0-9]+&mode=[az]+searchterm=(.*)+|productid=[0-9]+)$" ) { limit_req zone=one burst=5; set "${$deny_access}on"; } if ($deny_access = onon) { return 503; } location /deny_access{ } error_page 503 @deny_access; location @deny_access { rewrite ^(.*)$ /deny.html break; } .. } .. } 

您也可以使用limit_req模块来限制每个源IP可以访问特定path的速率。

http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#return :«…此外,非标准代码444在不发送响应标题的情况下closures连接 。 …»

– 所以你只需定义那些特定的位置并在那里使用return 444

任何其他build议来限制特定页面每秒/分钟的连接数量?

mgorven 已经指出你了。