In Apache I have a bunch of rules to block certain unwanted requests.

# 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]

What is the most efficient way to accomplish the same task in nginx?

link|improve this question

feedback

1 Answer

up vote 1 down vote accepted
# 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;
}

~ is a case-sensitive match, ~* is case-insensitive, & !~ & !~* are their negations.

Unfortunately neither && nor || are valid in nginx if statements, so nesting is the way to go.

link|improve this answer
1  
I don't know if nginx can support nested if statements. So note workarounds like this rosslawley.co.uk/2010/01/… – SaveTheRbtz Jul 30 '10 at 7:42
Nginx doesn't support nested statement. +1 for the answer. – Simone Carletti Jul 31 '10 at 10:07
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.