Formatting NGINX access logs when running in docker

18 views Asked by At

I'm running NGINX inside docker container. By default NGINX logs to stdout so running docker logs container-name gives you the contents of NGINX access log.

I'm trying to change the default access log format for NGINX but somehow I can only define additional access log that will print to stdout instead of reformating the default one (Both get printed). I tried different combinations of NGINX conf files but nothing works for me.

This is my nginx.conf file contents

user www-data;
worker_processes 1;

error_log   /var/log/nginx/error.log warn;
pid         /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    
        #default log format
        log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                                              '$status $body_bytes_sent "$http_referer" '
                                         '"$http_user_agent" "$http_cf_connecting_ip"';


    access_log off; 


    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    sendfile            on;
    keepalive_timeout   65;
    set_real_ip_from 127.0.0.1;
    real_ip_header CF-Connecting-IP;
    include /etc/nginx/sites-enabled/*.conf;
}

and this additional conf file is included in include /etc/nginx/sites-enabled/*.conf; line:

server {
    listen 80;

    root        /app;
    index       index.php;
    server_name automad-docker.local;

    client_max_body_size 100M;

        #default log format
        access_log /dev/stdout main;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        fastcgi_pass    127.0.0.1:9000;
        fastcgi_index   index.php;
        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include         fastcgi_params;
    }
}

What i get in the logs:

172.17.0.1 - - [22/Mar/2024:18:36:37 +0000] "GET / HTTP/1.1" 200 55020 "-" "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Mobile Safari/537.36" "101.35.***.***"
127.0.0.1 -  22/Mar/2024:18:36:37 +0000 "GET /index.php" 200

What I want to get in the logs:

172.17.0.1 - - [22/Mar/2024:18:36:37 +0000] "GET / HTTP/1.1" 200 55020 "-" "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Mobile Safari/537.36" "101.35.***.***"
0

There are 0 answers