NGINX: access logs – log all except 200 responses

Arseny Zinchenko - Mar 22 '19 - - Dev Community

The task is to save to a log file all requests excluding ones with the 200 response from the NGINX.

Using the approach described below with the map you can do various things, for example – chose a location to be used based on a header value. Will add such an example later.

For now, we are interested in two NGINX abilities: the conditional logging to choose when need to save an event to a log file and the map to set a variable’s value based on other variables.

map

Add the $abnormal variable in the http {} block and set its value to 0 – if a response has 200 value or default value 1 – if not:



http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    map $status $abnormal {
        ~^200  0;
        default 1;
    }
    ...


Enter fullscreen mode Exit fullscreen mode

Conditional logging

Next, in the server {} set condition to be used for the access.log using the if condition: if the $abnormal variable’s value is zero – then log nothing, if 1 – to save this response to the log:



server {
    charset utf-8;
    listen 80 default_server;
    server_name stage.example.com;
    if ($http_x_forwarded_proto = 'http') {
        return 301 https://stage.example.com$request_uri;
    }
    set $root_path /data/projects/frontend/web;
    root $root_path;
    index index.php;
    error_log /var/log/nginx/stage.example.com-error.log;
    access_log /var/log/nginx/stage.example.com-access.log combined if=$abnormal;
...


Enter fullscreen mode Exit fullscreen mode

Check it:

127.0.0.1 - - [15/Mar/2019:14:51:37 +0200] "GET /nginx_status HTTP/1.1" 404 153 "-" "Go-http-client/1.1"

127.0.0.1 - - [15/Mar/2019:14:51:52 +0200] "GET /nginx_status HTTP/1.1" 404 153 "-" "Go-http-client/1.1"

Done.

Similar posts

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player