无法在nginx日志中记录$ request_body

时间:2016-08-19 17:04:56

标签: nginx

我在让nginx有条件地记录请求正文时遇到了一些麻烦。我不想记录登录凭据,因此包含了当URI不是登录URI时正确触发的映射。似乎有一些类似的帖子建议使用

  • client_body_in_single_buffer on
  • fastcgi_pass php_cgi
  • echo_read_request_body

但这些似乎都没有帮助。有任何想法吗?以下是当前配置。

map $request_uri $log_body {

  ~/login 0;
  default 1;
}

log_format filter '$remote_addr - $remote_user [$time_local] '
  '"$request" $status $request_time $upstream_response_time "$scrubbed_request"';

server {
  listen 443 ssl;
  server_name localhost;

  ssl_certificate /etc/ssl/site.crt;
  ssl_certificate_key /etc/ssl/site.key;

  access_log /var/log/nginx/site.log filter;

  location /branch/dev/v2/ {

    set $scrubbed_request 'NOT LOGGED';
    if ($log_body) {
      set $scrubbed_request $request_body;
    }   
    rewrite /branch/dev/(.*) /$1 break;
    proxy_pass http://127.0.0.1:3001;
  }
  location /branch/dev/ {
    rewrite /branch/dev/(.*) /$1 break;
    proxy_pass http://127.0.0.1:4001;
  }
}

请注意,如果$scrubbed_request在log_format行中替换为$request_body,我们会捕获所有请求正文。这让我觉得set语句以某种方式以某种意外的方式消耗了request_body

1 个答案:

答案 0 :(得分:0)

read the documentation始终是个好主意:

  

当请求正文被读取到memory buffer时,变量的值在proxy_pass,fastcgi_pass,uwsgi_pass和scgi_pass指令处理的位置可用。

正如您所看到的,您必须将client_body_buffer_size配置得足以将整个身体存储在内存中。

您也不需要set语句,可以使用the if parameter of access_log和不同的格式。

相关问题