NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
z069
V2EX  ›  NGINX

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

  •  
  •   z069 · Apr 4, 2019 · 3631 views
    This topic created in 2606 days ago, the information mentioned may be changed or developed.
    因为图片加了参数适配缩略图,导致日志中存在大量的图片的访问记录,怎么才能设置不记录这些带有参数的日志呢?

    比如这个:
    /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 replies    2019-04-09 10:53:04 +08:00
    ryd994
        1
    ryd994  
       Apr 5, 2019 via Android
    你这个 URL 合法吗?
    &是保留字符,用于分割 args
    z069
        2
    z069  
    OP
       Apr 5, 2019
    @ryd994 刚才看了一下,确实不全,实际是 /thumbnail.php?src=https://www.domain.com/pic/pic.png&w=280&h=210&a=&zc=1
    ryd994
        3
    ryd994  
       Apr 5, 2019 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
       Apr 9, 2019
    @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 后提示通过,麻烦看下,配置哪有问题吗?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2671 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 77ms · UTC 15:40 · PVG 23:40 · LAX 08:40 · JFK 11:40
    ♥ Do have faith in what you're doing.