nginx:如何禁用 ($http_status == 200) 的 access_log

mda*_*yan 4 nginx http-status-code

有没有办法禁用access_log200 HTTP 响应代码?我尝试使用条件,但似乎块access_log中不允许使用指令if。我试过这个:

access_log /var/log/nginx/access_log;

if ($http_status == 200){
    access_log off;   # <---------
}
Run Code Online (Sandbox Code Playgroud)

但它无效

# nginx -t    
nginx: [emerg] "access_log" directive is not allowed here ...
Run Code Online (Sandbox Code Playgroud)

有什么办法可以做到这一点吗?

小智 6

请尝试

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

access_log /var/log/nginx/access_log combined if=$loggable;
Run Code Online (Sandbox Code Playgroud)

这会导致响应代码为 2xx 的请求不被记录。

ngx_http_log_module 文档