nginx 访问日志忽略某些请求

nginx 访问日志忽略某些请求

我在 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

以下是我在 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 "-" "-"

答案1

您现在可以这样做

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;

}

答案2

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

就像是

location /test.html {
  access_log off;
}

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

答案3

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

不过,您可以尝试一些技巧。这些技巧都会导致您不感兴趣的数据被记录到另一个文件中,您可以根据需要从外部删除该文件(记得之后 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 变量

除此之外,您已经获得了源代码,因此可以破解您需要的内容。

相关内容