0

Is it possible to have two separate log files, each saving logs with different format?

For example, will something like this work?

events{}
# See blow link for Creating NGINX Plus and NGINX Configuration Files 
# https://docs.nginx.com/nginx/admin-guide/basic-functionality/managing-configuration-files/
http {

    include /etc/nginx/mime.types;
    default_type  application/octet-stream;

    access_log  /var/log/nginx/access1.log;

    log_format  lokari  '"$time_local" "$remote_addr" "$status" "$bytes_sent" "$request_time" "$request_method" "$request_uri" "$server_protocol"';

    # The identifier Backend is internal to nginx, and used to name this specific upstream
    upstream backend {
    # BACKEND_HOST is the internal DNS name used by the Backend Service inside the Kubernetes cluster 
    # or in the services list of the docker-compose. 
    server ${BACKEND_HOST}:${BACKEND_PORT};
    }

    server {
        listen ${NODE_PORT};
        root /usr/share/nginx/html;
        index index.html;

        access_log /var/log/nginx/access2.log lokari;

        error_log  /var/log/nginx/error.log debug;

        location / {
        try_files $uri $uri/ /index.html;
        }

        location /api/ {
        resolver 127.0.0.11; 
        #nginx will not crash if host is not found    
        # The following statement will proxy traffic to the upstream
        proxy_pass http://backend;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }

}

So, I want to have two access logs, the access1.log with the default format and the access2.log with a custom format.

Kosmylo
  • 125
  • 7

1 Answers1

1

Nginx will inherit access log settings from the previous configuration level only if they won't be redefined at the current level. Adding

access_log /var/log/nginx/access2.log lokari;

line to the server configuration level will made nginx to not inherit the previous

access_log /var/log/nginx/access1.log;

settings and the only way to get two access logs for this server block is to define them both:

server {
    ...
    access_log /var/log/nginx/access1.log;
    access_log /var/log/nginx/access2.log lokari;
    ...
Ivan Shatsky
  • 2,726
  • 2
  • 7
  • 19