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

2017-09-23 00:51:41 +08:00
 a523860
if ($http_user_agent ~* (abcd|123456789)) {
access_log off; }
这样不行

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

要怎么写,我想设置不记录 某些 http_user_agent 访问的日志
5583 次点击
所在节点    NGINX
4 条回复
akira
2017-09-23 01:27:10 +08:00
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
2017-09-23 06:53:29 +08:00
有个 nginx_log 插件,可以实现
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
2017-09-23 10:43:47 +08:00
放在 location 里是没问题的,可能是正则没写好?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/392977

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX