两个具有不同格式的单独 access.log 文件

两个具有不同格式的单独 access.log 文件

是否可以有两个单独的日志文件,每个文件保存不同格式的日志?

例如,这样的事情会起作用吗?

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;
        }
    }

}

因此,我想要两个访问日志,一个access1.log具有默认格式,另一个access2.log具有自定义格式。

答案1

Nginx 将从上一个配置级别继承访问日志设置,前提是这些设置不会在当前级别重新定义。添加

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

行到服务器配置级别将使 nginx 不继承以前的

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

设置,获取此服务器块的两个访问日志的唯一方法是定义它们:

server {
    ...
    access_log /var/log/nginx/access1.log;
    access_log /var/log/nginx/access2.log lokari;
    ...

相关内容