3

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?

scetoaux
  • 1,269
  • 2
  • 12
  • 25
mdaliyan
  • 33
  • 1
  • 5
  • 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 : http://nginx.org/en/docs/http/ngx_http_log_module.html#access_log – mightyteja Mar 14 '19 at 09:14
  • @mightyteja thanks a lot. that worked fine. but why did you commented instead of posting an answer? – mdaliyan Mar 14 '19 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 '19 at 11:24

1 Answers1

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

eMPee584
  • 115
  • 4
mightyteja
  • 431
  • 3
  • 13