在Apache中,我有一堆规则来阻止某些不需要的请求。
# Block libwww-perl/5.805 from attempting to exploit security vulnerabilities RewriteCond %{HTTP_USER_AGENT} ^libwww\-perl/.+ [NC] RewriteCond %{QUERY_STRING} . RewriteRule . - [F,L] # Block generic Java-based clients RewriteCond %{HTTP_USER_AGENT} ^Java/1\.6\.0_04 [NC] RewriteRule . - [F,L] # Block generic Mozilla clients RewriteCond %{HTTP_USER_AGENT} ^Mozilla/5\.0$ [NC] RewriteRule . - [F,L]
什么是在nginx中完成同样任务的最有效的方法?
# Block libwww-perl/5.805 from attempting to exploit security vulnerabilities if ($http_user_agent ~* ^libwww\-perl/.+) { if ($query_string ~ \.) { return 403; } } # Block generic Java-based clients if ($http_user_agent ~* ^Java/1\.6\.0_04) { return 403; } # Block generic Mozilla clients if ($http_user_agent ~* ^Mozilla/5\.0$) { return 403; }
~是区分大小写的匹配, ~*是不区分大小写的,& !~ & !~*是它们的否定。
不幸的是&&也没有|| 在nginx中if语句是有效的,所以嵌套就是要走的路。