Nginx logs are not shown

Viewed 1941

I have a docker running nginx with port forward to 8080

All I want is to see the logs in nginx when I hit

curl http://localhost:8080

(I get an answer but I want to see logs)

Both access.log and error.log and enabled from /etc/nginx/nginx.conf

user  nginx;
worker_processes  1;

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


events {
    worker_connections  1024;
}


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

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

Neither tail -f access.log nor cat access.log works. All I get is an empty line:

root@fe0f47477548:/var/log/nginx# cat access.log

^C
root@fe0f47477548:/var/log/nginx#
1 Answers

The NGINX docker distribution redirects (soft links) logging to the stdout/stderr

lrwxrwxrwx 1 root root 11 Feb 17 19:20 access.log -> /dev/stdout
lrwxrwxrwx 1 root root 11 Feb 17 19:20 error.log -> /dev/stderr

From Docker host

You can access the logs using Docker logs

docker logs nginx

From inside the Docker container

It is possible to remove the log forwarding which is defined in the standard NGINX Docker Image.
This requires creating a custom Dockerfile (ie mynginx) but it is a simple change

FROM nginx:latest

# drop symlinks
RUN unlink /var/log/nginx/access.log
RUN unlink /var/log/nginx/error.log

When running the newly defined container the log files can be seen in /var/logs/nginx

-rw-r--r-- 1 root root 491 Feb 19 13:14 access.log
-rw-r--r-- 1 root root 0 Feb 19 13:14 error.log

root@ca2472f28be5:/var/log/nginx# cat access.log
72.25.0.1 - - [19/Feb/2021:13:15:55 +0000] "GET 
/static/js/main.f81cbe58.chunk.js HTTP/1.1" 304 0 "http://localhost/" 
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 
(KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36" "-"
Related