nginx 访问日志忽略某些请求

Mic*_*ano 9 nginx log-files haproxy

我在 haproxy 后面运行 nginx(在同一台服务器上运行)。我已将 haproxy 配置为在 nginx 上使用一个简单的 html 文件来验证服务是否已启动,因为我在此主机上没有/不想要有效的“/”URL。Nginx 不支持 OPTIONS 请求类型(据我所知)这是 haproxy 使用的默认值,因此我将其更改为 GET。

由于我在 nginx 中打开了访问日志,因此我在访问日志中收到了所有这些正常运行时间轮询请求。有没有办法可以将 nginx 配置为忽略某些请求,并跳过记录它们?

这是 haproxy 后端:

backend static_http
        option  httpchk GET /test.html
        option  redispatch
        balance roundrobin
        #fullconn 1000
        server  w1_static www1:81 check port 81 inter 2000
Run Code Online (Sandbox Code Playgroud)

这是我在 nginx 日志中看到的:

127.0.0.1 - - [24/Jul/2009:19:28:22 +0000] "GET /test.html HTTP/1.0" 200 12 "-" "-"
127.0.0.1 - - [24/Jul/2009:19:28:24 +0000] "GET /test.html HTTP/1.0" 200 12 "-" "-"
127.0.0.1 - - [24/Jul/2009:19:28:26 +0000] "GET /test.html HTTP/1.0" 200 12 "-" "-"
127.0.0.1 - - [24/Jul/2009:19:28:28 +0000] "GET /test.html HTTP/1.0" 200 12 "-" "-"
127.0.0.1 - - [24/Jul/2009:19:28:30 +0000] "GET /test.html HTTP/1.0" 200 12 "-" "-"
Run Code Online (Sandbox Code Playgroud)

KCD*_*KCD 10

你现在可以这样做

http://nginx.org/en/docs/http/ngx_http_log_module.html

if 参数 (1.7.0) 启用条件记录。如果条件评估为“0”或空字符串,则不会记录请求

map $request_uri $loggable {
  / 0;
  /healthcheck.html 0;
  default 1;
}

server {
...
  access_log /var/log/nginx/access.log combined if=$loggable;

}
Run Code Online (Sandbox Code Playgroud)


Jau*_* Ho 7

好吧,您可以尝试为此设置特定的位置指令。

就像是

location /test.html {
  access_log off;
}
Run Code Online (Sandbox Code Playgroud)

应该工作(未经测试)...


Jam*_*s F 0

没有内置的过滤功能可以满足您的要求。

不过,您可以尝试一些技巧。这些都会导致您不感兴趣的数据被记录到不同的文件中,您可以根据您认为适当的频率从外部删除该文件(记住随后使用 HUP nginx)。

  • 如果 haproxy 是唯一请求 /test.html 的东西,则每个位置块可以有不同的访问日志(不幸的是,不适用于位置块内的 if 块)。

  • 如果其他节点请求 /test.html 但来自 127.0.0.1 的所有请求都是 haproxy,您可以基于 $remote_addr 创建一个映射,将 127.0.0.1 转换为 access_log.junk 并将其他所有内容转换为 access_log。在访问日志语句的名称中使用map变量

如果做不到这一点,您已经获得了源代码,因此可以破解您需要的内容。