How to block Nginx access log statements from specific user agents

Solution 1:

Try this:

# map goes *outside* of the "server" block
map $http_user_agent $ignore_ua {
    default                 0;
    "~Pingdom.*"            1;
    "ELB-HealthChecker/1.0" 1;
}

server {
    # Things omitted for brevity

    location / {
        if ($ignore_ua) {
            access_log off;
            return 200;
        }
    }
}    

The if part would probably need to be integrated into your appropriate location block.

Relevant nginx documentation: map, if, access_log

Solution 2:

Thanks to @gnarfoz

Respect to if is Evil, This is better way to use condition before log

map $http_user_agent $ignore_ua {
    default                 0;
    "~Pingdom.*"            1;
}

server {
    location / {
        access_log /var/log/nginx/access.log if=$ignore_ua;
    }
}  

NGINX Documentation Enabling Conditional Logging