Disable Nginx logging of 403 errors

1.2k Views Asked by At

Is it possible to stop Nginx logging 403 errors?

I am using the directive:

error_log /var/www/error.log;

You can use log_not_found to stop the logging of 404s, but I can't see a way to stop the logging of 403s.

2

There are 2 best solutions below

1
Danila Vershinin On BEST ANSWER

Theoretically, you can specify a named location to handle 403 and disable error logging there:

error_page 403 = @403_handler;
location @403_handler {
    error_log /dev/null;
}

Doesn't have to be a named location though:

error_page 403 /403.html;
location = /403.html {
    error_log /dev/null;
}
0
lucian On

The accepted answer was not working for me.

I fixed this by adding a slash at the end of the index directive like so:

index index.htm index.html index.php /;

The way this works is I guess by looking for nothing in the root directory, not finding it and reporting a genuine 404. It can be any bogus page with a slash before, such as /404.html or /this_file_isnt_really_there or whatever.

What it does is to suppress 403 errors altogether, as if the directory weren't there at all, so there's no reason to log.

This comes with the advantage of hardening the installation, actual folder paths can no longer be identified.

However, I'm not sure how valid, safe or futureproof this approach may be, so use at your own peril. Something like log_not_allowed would be much nicer i guess.