nginx log 404 on different log file

How can i get all 404 errors from all the sites into one log? I only know how to do it on one domain and only when i change the default 404 page.

Here is how i did it on one domain

    error_page 404 /404.html;
    location = /404.html {
            root /home/awesomename;
            access_log /home/custom_logs/404.log;
            internal;
    }

Is there a way to do it without changing anything else?


Solution 1:

The if parameter (1.7.0+) enables conditional logging. A request will not be logged if the condition evaluates to “0” or an empty string. In the following example, the requests with response codes 4xx will be logged:

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

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