0

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?

2
# 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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