简体   繁体   English

Nginx位置或地图禁用特定GET参数的访问日志

[英]Nginx location or map to disable access log for specific GET argument

I've been trying to filter out a bunch of lines from the nginx access log. 我一直在尝试从nginx访问日志中过滤掉很多行。 These lines contain a specific parameter, so I thought it would be easy to filter them out. 这些行包含一个特定的参数,因此我认为将它们过滤掉很容易。 Examples of the requests: 请求示例:

/index.php?cmd=thumb&#####
/index.php?cmd=preview&#####

where ##### can be anything. #####可以是任何东西。 /index.php without this 'cmd' parameter (but possibly with other parameters) SHOULD be logged however. /index.php没有这个'cmd'参数(但是可能还有其他参数)应该被记录。

I have tried the following 我尝试了以下

server {
    location ~ /index\.php\?cmd.* {
        access_log off;
    }
}

My guess is that location only looks at the file on the server you need, not any of the reuqest parameters. 我的猜测是,该位置仅查看您需要的服务器上的文件,而不查看任何reuqest参数。 That could, for example, explain why location ~* \\.(?:css|js)$ {access_log off;} (notice the $ ) also blocks logging request like /something.js?v=blahblah . 例如,这可以解释为什么location ~* \\.(?:css|js)$ {access_log off;} (注意$ )也会阻止日志记录请求,如/something.js?v=blahblah As such, my first option would be invalid for sure. 因此,我的第一个选择肯定是无效的。 This is confirmed below (with source ). 这已在下面得到确认(带有来源 )。

or 要么

server {
    location /index.php {
        if ($arg_cmd) {
            access_log off;
        }
    }
}

or 要么

http {
    map $request_uri $log {
        ~ /index\.php\?cmd.* 0;
        default 1;
    }

    access_log /var/log/nginx/access.log combined if=$log;
}

So far, all of the above do not have the desired result. 到目前为止,上述所有方法均未达到预期的效果。 I'm still convinced this should be possible and that I did something wrong in all of these implementations. 我仍然坚信这是可能的,并且在所有这些实现中我做错了什么。 Could someone please help me out? 有人可以帮我吗?

  1. First option was never going to work (see directly below it). 第一种选择永远不会起作用(直接在其下方)。
  2. Second option will only work if the value of the argument evaluates to TRUE. 仅当参数值计算为TRUE时,第二个选项才有效。 I had no control over the value itself (it was a hash) in my case, so this could not work for me. 在我的情况下,我无法控制值本身(它是一个哈希值),因此这对我来说不起作用。 In my case, another option would be if ($args ~ ^cmd=.+) {access_log off;} if cmd is always the first argument, or just if ($args ~ cmd) {access_log off;} . 在我的情况下,另一个选择是if ($args ~ ^cmd=.+) {access_log off;}如果cmd始终是第一个参数,或者仅仅if ($args ~ cmd) {access_log off;} (thx Richard Smith) (向理查德·史密斯致敬)
  3. I found out that the third option WILL work by removing the space between ~ and /index\\.php\\?cmd.* 0; 我发现第三个选项将通过删除~/index\\.php\\?cmd.* 0;之间的空格来/index\\.php\\?cmd.* 0; This one allows me to globally set some more conditions to ignore, so I'm sticking with this one. 这使我可以全局设置更多要忽略的条件,因此我坚持使用这一条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM