Logs

Contents

Logs#

This page explores methods to manage logs in Nginx.

Nginx provides tools for configuring and handling logs:

The following sections detail the features and configurations for effective log management.

docker run -itd --name logs_nginx --rm -p 80:80 nginx
b9eb474f45b7f35e03f2316626428e11b397171849622cb7f6a30f52798e2515

Clean the environment from created container.

docker stop logs_nginx
logs_nginx

Several logs#

You can define multiple files for logs. Each logging configuration is applied to it’s context and to all selected contexts, except if a specific logging configuration is defined in a nested context.


The following cell defines nginx with two roots. There is a general config for the whole server and a specific config for /my_rote.

docker exec -i logs_nginx sh -c 'cat >  /etc/nginx/nginx.conf' <<EOF
events {}
http {server{
    access_log /basic_log;
    location / {
        return 200 "hello";
    }

    location /my_rote {
        access_log /my_rote_log;
        return 200 "hello";
    }
}}
EOF
docker exec -i logs_nginx nginx -s reload
2024/12/20 07:22:05 [notice] 68#68: signal process started

Now we’ll try a requests to that log:

curl localhost:80
curl localhost:80/my_rote
hello
hello

Both rotes works lets check results in the config.

docker exec -it logs_nginx cat /basic_log
172.17.0.1 - - [20/Dec/2024:07:22:05 +0000] "GET / HTTP/1.1" 200 5 "-" "curl/7.81.0"
docker exec -it logs_nginx cat /my_rote_log
172.17.0.1 - - [20/Dec/2024:07:22:05 +0000] "GET /my_rote HTTP/1.1" 200 5 "-" "curl/7.81.0"

Each file has a record that corresponds to what the log has been configured for.