如何阻止来自特定用户代理的 Nginx 访问日志语句

bri*_*nyc 8 monitoring configuration nginx logging amazon-elb

我希望关闭来自 http 用户代理的特定请求的 Nginx 访问日志文件的登录。

基本上来自 Amazon ELB 健康检查和我们的外部 (Pingdom) 监控。由于这些每隔几秒钟就会出现一次,因此很难对日志进行排序。

"GET / HTTP/1.1" 200 727 "-" "ELB-HealthChecker/1.0"
"GET /login HTTP/1.1" 200 7492 "-" "Pingdom.com_bot_version_1.4_(http://www.pingdom.com/)"
Run Code Online (Sandbox Code Playgroud)

我能够阻止图像文件的日志记录,但没有看到传入请求的任何内容:

location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml|svg)$ {
        access_log        off;
        expires           30d;
}
Run Code Online (Sandbox Code Playgroud)

提前致谢!


所以我尝试了@Gnarfoz 的推荐,但有一些有趣的副作用。虽然没有记录这两个“健康检查”,但 Pingdom 开始将服务器识别为 DOWN,即使它已启动并正在运行。这很有趣,因为负载均衡器没有,如果它有,它就会删除我们正在测试的节点。

我将 MAP 部分放在日志下方的 HTML 块中:

access_log /www/access.log;
error_log /www/error.log;

map $http_user_agent $ignore_ua {
            default                 0;
            "~Pingdom.*"            1;
            "ELB-HealthChecker/1.0" 1;
    }
Run Code Online (Sandbox Code Playgroud)

我将 IF 语句放在我的服务器块中,使用默认位置:

location / {
                try_files $uri $uri/ /index.php?$args;

                if ($ignore_ua) {
                       access_log off;
                }
        }
Run Code Online (Sandbox Code Playgroud)

当我这样做时,Pingdom 开始在错误日志文件中生成 404 错误:

2012/08/03 17:10:33 [error] 6250#0: *164 open() "/www/public_html/login" failed (2: No such file or directory), client: 10.xx.xx.xx, server: xxx.com, request: "GET /login HTTP/1.1", host: "xxx.com"
2012/08/03 17:11:32 [error] 6250#0: *240 open() "/www/public_html/login" failed (2: No such file or directory), client: 10.xx.xx.xx, server: xxx.com, request: "GET /login HTTP/1.1", host: "xxx.com"
Run Code Online (Sandbox Code Playgroud)

Gna*_*foz 8

尝试这个:

# 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;
        }
    }
}    
Run Code Online (Sandbox Code Playgroud)

if 部分可能需要集成到您适当的位置块中。

nginx 相关文档: map , if , access_log