V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
z069
V2EX  ›  NGINX

怎么设置 nginx 日志不记录带参数的图片?

  •  
  •   z069 · 2019-04-04 23:43:47 +08:00 · 2576 次点击
    这是一个创建于 1841 天前的主题,其中的信息可能已经有所发展或是发生改变。
    因为图片加了参数适配缩略图,导致日志中存在大量的图片的访问记录,怎么才能设置不记录这些带有参数的日志呢?

    比如这个:
    /pic.png&w=280&h=210&a=&zc=1


    我设置了:
    location ~ .*\.(png&w=280&h=210&a=&zc=1)$ {
    expires 30d;
    access_log off;
    }

    location ~ .*\.(png)$ {
    expires 30d;
    access_log off;
    }

    但是貌似没有效果,依然会记录,求教各位大佬,这个有什么办法设置么?
    4 条回复    2019-04-09 10:53:04 +08:00
    ryd994
        1
    ryd994  
       2019-04-05 00:10:11 +08:00 via Android
    你这个 URL 合法吗?
    &是保留字符,用于分割 args
    z069
        2
    z069  
    OP
       2019-04-05 00:19:18 +08:00
    @ryd994 刚才看了一下,确实不全,实际是 /thumbnail.php?src=https://www.domain.com/pic/pic.png&w=280&h=210&a=&zc=1
    ryd994
        3
    ryd994  
       2019-04-05 00:31:56 +08:00 via Android
    location 只匹配 path 部分,不匹配整个 URL
    map $arg_src $log_filter {
    default 1;
    ~ .*\.(png)$ 0;
    }

    access_log 照抄原来的,官方 manual 里也能查到默认的。在最后加上 if=$log_filter

    思路就是这样,剩下的 rtfm:
    http://nginx.org/en/docs/http/ngx_http_log_module.html
    http://nginx.org/en/docs/http/ngx_http_map_module.html
    z069
        4
    z069  
    OP
       2019-04-09 10:53:04 +08:00
    @ryd994 你好,感谢您的解答

    按照您的提示,我在 http 中添加了
    map $arg_src $log_filter {
    default 1;
    ~ .*\.(png)$ 0;
    }
    access_log /data/wwwlogs/domain.com.log combined if=$log_filter;
    测试提示[emerg] invalid number of the map parameters


    按照文档中的示例,
    添加在同一位置的:
    map $status $loggable {
    ~^[403] 0;
    default 1;
    }
    access_log /data/wwwlogs/domain.com.log combined if=$loggable;
    这个 nginx -t 后提示通过,麻烦看下,配置哪有问题吗?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5632 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 02:02 · PVG 10:02 · LAX 19:02 · JFK 22:02
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.