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

nginx 这样设置不记录某种日志,为什么不生效

  •  
  •   a523860 · 2017-09-23 00:51:41 +08:00 · 5549 次点击
    这是一个创建于 2379 天前的主题,其中的信息可能已经有所发展或是发生改变。
    if ($http_user_agent ~* (abcd|123456789)) {
    access_log off; }
    这样不行

    location / { if ($http_user_agent ~* (abcd|123456789)) {
    access_log off; }
    }
    这样也不行

    要怎么写,我想设置不记录 某些 http_user_agent 访问的日志
    4 条回复    2017-09-23 10:43:47 +08:00
    akira
        1
    akira  
       2017-09-23 01:27:10 +08:00   ❤️ 1
    access_log 的 off 只影响本层。试试用 access_log if=condition 格式
    map $status $loggable {
    abcd 0;
    123456789 0;
    default 1;
    }

    access_log /var/log/nginx/access.log combined if=$loggable;
    fenglangjuxu
        2
    fenglangjuxu  
       2017-09-23 06:53:29 +08:00 via iPhone
    有个 nginx_log 插件,可以实现
    lerry
        3
    lerry  
       2017-09-23 07:24:57 +08:00
    location / {
    if ($http_user_agent ~* (Chrome\/50\.0\.2661\.102|MSIE\ 9\.0) ) {
    access_log off;
    return 403;
    }
    }

    我这样写是好使的,注意转义
    ayiis
        4
    ayiis  
       2017-09-23 10:43:47 +08:00
    放在 location 里是没问题的,可能是正则没写好?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1015 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 19:25 · PVG 03:25 · LAX 12:25 · JFK 15:25
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.