4

Is there any way to disable access_log for 200 HTTP response code? I tried using conditions but it seems access_log directive is not allowed in an if block. I tried this:

access_log /var/log/nginx/access_log;

if ($http_status == 200){
    access_log off;   # <---------
}

but it is not valid

# nginx -t    
nginx: [emerg] "access_log" directive is not allowed here ...

Is there any way to do this?

3
  • In the following example, the requests with response codes 2xx will not be logged: map $status $loggable { ~^[2] 0; default 1; } access_log /var/log/nginx/access_log combined if=$loggable; Src : nginx.org/en/docs/http/ngx_http_log_module.html#access_log
    – mightyteja
    Mar 14, 2019 at 9:14
  • @mightyteja thanks a lot. that worked fine. but why did you commented instead of posting an answer?
    – mdaliyan
    Mar 14, 2019 at 10:25
  • You are welcome. I just dont want to flood the question if its not the right one. Moreover it will stay as a troubleshooting step. Updated it as answer. If the answer does solve your query, please accept the answer so that other who view the thread later would find it useful.
    – mightyteja
    Mar 14, 2019 at 11:24

1 Answer 1

6

Please try

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

access_log /var/log/nginx/access_log combined if=$loggable;

which causes requests with response codes 2xx not to be logged.

ngx_http_log_module documentation

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .