1

I'm having nginx version: openresty/1.5.12.1.

Is there a way to log only the 4xx and 5xx logs to access_log or to an additional access_log file?

What I mean is, these logs should be there in the default access_log file. Additionally, they should be logged in the custom log file as well. Logging only 4xx and 5xx is also good.

Thanks !

Berlin
  • 1,456
  • 1
  • 21
  • 43
  • Why not to use **error_log**? Do you really need to customise standard (which is not possible currently) error log format? – Anatoly Sep 14 '15 at 08:05

3 Answers3

1

No, it does not appear this was possible with nginx 1.5.x.

Yes, it is possible to do this in nginx.conf files as of nginx 1.7.0 (http://nginx.org/en/docs/http/ngx_http_log_module.html#access_log):

nginx access_log directive 'if' parameter

From https://docs.nginx.com/nginx/admin-guide/monitoring/logging/#conditional :

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

access_log /path/to/access.log combined if=$loggable;

That will skip logging all 2xx and 3xx status codes. You could rewrite this to positively select 4xx and 5xx status codes (though these will largely be equivalent unless you are returning large volumes of 1xx status codes or returning non-standard > 599 status codes, which would be odd):

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

access_log /path/to/access.log combined if=$loggable;
huntharo
  • 2,616
  • 21
  • 23
-1

Without editing the source or writing an Nginx 3rd party module their is no way to do that (as far as I know and had seen).

Why not just run the log through grep or such?

A great little log parsing tutorial tha may help you: https://rtcamp.com/tutorials/nginx/log-parsing/

0xGiddi
  • 404
  • 2
  • 12
-1

grep " 404 " access.log | sed -e 's/.GET //' -e 's/\ .//'| sort | uniq -c | sort -n | gawk '{print $2","$1}'