Search code examples
nginx

Nginx prints 404 log in access.log


Recently I added log_not_found directive in my Nginx config. Works fine and prevents Nginx to print 404 logs on error.log but still prints 404 log on access.log.

How to disable Nginx 404 logs on access.log?

Sample log

172.18.xx.xx - - [02/Sep/2024:11:38:00 +0000] "GET /test HTTP/2.0" 404 131 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36" "219.100.xx.xx"

Nginx Config

http {
    log_format main
        '$remote_addr - $remote_user [$time_local] "$request" '
        '$status $body_bytes_sent "$http_referer" '
        '"$http_user_agent" "$http_x_forwarded_for"';

    server {
        listen 443 ssl;
        root /example/path/html;
        log_not_found off;

        access_log /var/log/nginx/access.log main;
        error_log /var/log/nginx/error.log warn;

        location / {
            add_header "Access-Control-Allow-Origin" "*" always;
            try_files $uri =404;
        }
    }
}

Solution

  • Actually Nginx is not doing anything wrong. Every request should be logged in access.log.

    Currently I use access_log to completely turn off the logs in a location.

    location /favicon.ico {
        access_log off;
    }
    

    In addition to this workaround, I submitted a feature request to Nginx GitHub repository.