如何在nginx访问日志中不记录get request参数?

Dom*_*ino 10 nginx access-log

我要求启用访问日志,但出于合规性原因,无法在访问日志中记录敏感的GET请求参数的数据.虽然我知道,我可以解析日志(事后)并对它们进行消毒,这不是一个可接受的解决方案 - 因为出于合规性原因,日志不能被篡改.

http://www.example.com/resource?param1=123&sensitive_param=sensitive_data

如何防止将"sensitive_data"参数值写入日志?以下是一些想法:

  • 发送POST请求 - 不是JSONP的选项.
  • 对"资源"使用新的位置规则,并设置访问日志以使用log_format,使用不同的格式(即不使用$ remote_addr).请参阅此参考:http://nginx.org/en/docs/http/ngx_http_log_module.html
  • 记录一个$ sanitized_remote_addr,然后设置它(以某种方式解析$ remote_addr或其他东西?),然后再进入日志.我们不确定这是否容易实现.

该怎么做?

Sha*_*wal 7

由于log_format模块只能在http级别config 上使用,因此先前的答案将不起作用。

要解决此问题,我们可以log_formatlocation指令中删除配置,并将其保留在http级别的config中。

http {

    log_format filter '$remote_addr - $remote_user [$time_local] '
        '"$temp" $status $body_bytes_sent "$http_referer" "$http_user_agent"';

    # Other Configs
}
Run Code Online (Sandbox Code Playgroud)

log_format指令可以在location指令块的后面定义变量。

因此最终配置将如下所示:

http {

    log_format filter '$remote_addr - $remote_user [$time_local] '
        '"$temp" $status $body_bytes_sent "$http_referer" "$http_user_agent"';

    # Other Configs

    server {
        #Server Configs
        location / {
            set $temp $request;
            if ($temp ~ (.*)password=[^&]*(.*)) { 
                set $temp $1password=****$2;
            }

            access_log /opt/current/log/nginx_access.log filter;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Don*_*hev 1

到目前为止我找到的解决方案就在这里。简而言之:

location /any_sensitive... {
    # Strip password in access.log
    set $temp $request;
    if ($temp ~ (.*)password=[^&]*(.*)) {
        set $temp $1password=****$2;
    }

    log_format filter '$remote_addr - $remote_user [$time_local] '
        '"$temp" $status $body_bytes_sent "$http_referer" "$http_user_agent"';

    access_log logs/access.log filter;
}
Run Code Online (Sandbox Code Playgroud)

也许这曾经在某个时候起作用,现在它说:

nginx: [emerg] unknown "temp" variable
Run Code Online (Sandbox Code Playgroud)

或者

nginx: [warn] the "log_format" directive may be used only on "http" level in ...
Run Code Online (Sandbox Code Playgroud)

  • 不,你说得对,只要你在某个地方设置了变量,nginx 就不应该抱怨未知变量。我将您的答案改编为工作解决方案,如下所述:https://github.com/sunlightlabs/congress/tree/master/config/nginx#suppressing-user-latitudelongitude-in-our-logs (3认同)