nginx 配置文件问题:根据请求的类型来过滤 access log

2018-12-29 14:40:13 +08:00
 MisakaTang

想要在 nginx 上屏蔽掉 OPTIONS 请求不写到 accesslog 里去,然后参照https://blog.rooot.me/p/nginx_access_log_filter.html这篇文章写了如下的配置:

set $logfile /log/xxxx.log ;
if ($request_method = OPTIONS ){
	set $logfile /dev/null ;
}
access_log /$logfile ;

但是配置不生效,求解答

2169 次点击
所在节点    NGINX
3 条回复
coolloves
2018-12-29 16:24:10 +08:00
```
以下放到 http 字段内
map $request_method $log{
OPTIONS 0 ;
default 1 ;
}

以下放到 server 下
access_log logs/access.log main if=$log;
```
coolloves
2018-12-29 16:39:30 +08:00
http://nginx.org/en/docs/http/ngx_http_log_module.html

The if parameter (1.7.0) enables conditional logging. A request will not be logged if the condition evaluates to “ 0 ” or an empty string. In the following example, the requests with response codes 2xx and 3xx will not be logged:

map $status $loggable {
~^[23] 0;
default 1;
}

access_log /path/to/access.log combined if=$loggable;
MisakaTang
2018-12-29 16:43:59 +08:00
@coolloves #2 感谢大佬 解决了

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

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

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

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

© 2021 V2EX