246

Is there a way to have the master process log to STDOUT STDERR instead of to a file?

It seems that you can only pass a filepath to the access_log directive:

access_log  /var/log/nginx/access.log

And the same goes for error_log:

error_log /var/log/nginx/error.log

I understand that this simply may not be a feature of nginx, I'd be interested in a concise solution that uses tail, for example. It is preferable though that it comes from the master process though because I am running nginx in the foreground.

2
  • 20
    Using Nginx inside a Docker container? Checkout this answer.
    – czerasz
    Commented Jan 30, 2016 at 21:34
  • 1
    The accepted answer (Patrick's) works for official Nginx Docker images (hub.docker.com/_/nginx).
    – Farshid T
    Commented May 12, 2018 at 10:51

8 Answers 8

318

Edit: it seems nginx now supports error_log stderr; as mentioned in Anon's answer.

You can send the logs to /dev/stdout. In nginx.conf:

daemon off;
error_log /dev/stdout info;

http {
  access_log /dev/stdout;
  ...
}

edit: May need to run ln -sf /proc/self/fd /dev/ if using running certain docker containers, then use /dev/fd/1 or /dev/fd/2

13
  • 4
    When I try to do this I get the following error: 2014/07/29 10:19:09 [emerg] 13742#0: open() "/dev/stdout" failed (13: Permission denied)
    – Jon Tirsen
    Commented Jul 29, 2014 at 10:19
  • 1
    Jon, what system are you on? On my system, /dev/stdout is world-readable symlink to /dev/fd/1 which is owned and read+writeable by my user.
    – Patrick
    Commented Jul 29, 2014 at 19:06
  • 2
    I'm seeing this fail with ENXIO when stdout is open to a socket rather than a file. There's an upstream kernel ticket indicating that this is deliberate and by intent: bugzilla.kernel.org/show_bug.cgi?id=1360 -- thus, while this answer is sufficient in some cases, it doesn't fully cover the range of possible failures. Commented Oct 21, 2014 at 22:53
  • 2
    Lost a few hours on this without any success. Tried to change everything (daemon modes, users, nginx versions etc). It just does not work for me. """open() "/dev/stderr" failed (6: No such device or address)""" (same issues with stdout, but nginx should output to stderr according to docs) Commented Feb 8, 2015 at 10:09
  • 3
    Do not use /dev/sdtout or /dev/stderr. Use stdout or stderr.
    – ErikE
    Commented Sep 22, 2020 at 0:04
95

If the question is docker related... the official nginx docker images do this by making softlinks towards stdout/stderr

RUN ln -sf /dev/stdout /var/log/nginx/access.log && ln -sf /dev/stderr /var/log/nginx/error.log

REF: https://microbadger.com/images/nginx

6
  • 13
    This answer is mostly correct, but sadly not for the alpine images (see github.com/nginxinc/docker-nginx/blob/master/stable/alpine/…), only for the others like jessie do use this statement. If you´re an alpine user, just create your own Dockerfile with FROM nginx:alpine RUN ln -sf /dev/stdout /var/log/nginx/access.log \ && ln -sf /dev/stderr /var/log/nginx/error.log CMD ["nginx-debug", "-g", "daemon off;"]
    – jonashackt
    Commented Mar 15, 2018 at 7:51
  • 2
    Patrick's answer works for official nginx images (hub.docker.com/_/nginx), both debian (latest) and alpine bases.
    – Farshid T
    Commented May 12, 2018 at 10:49
  • Your REF link appears to be dead.
    – peedee
    Commented Dec 14, 2018 at 9:55
  • 1
    @jonashackt I used alpine images and I found it also logged to stdout
    – Qiulang
    Commented May 14, 2019 at 10:00
  • 2
    @jonashackt New versions of alpine do work now github.com/nginxinc/docker-nginx/blob/master/stable/alpine/…
    – ch271828n
    Commented Jan 31, 2021 at 1:54
36
Syntax: error_log file | stderr | syslog:server=address[,parameter=value] | memory:size [debug | info | notice | warn | error | crit | alert | emerg];
Default:    
error_log logs/error.log error;
Context:    main, http, stream, server, location

http://nginx.org/en/docs/ngx_core_module.html#error_log

Don't use: /dev/stderr This will break your setup if you're going to use systemd-nspawn.

0
14

For a debug purpose:

/usr/sbin/nginx -g "daemon off;error_log /dev/stdout debug;"

For a classic purpose

/usr/sbin/nginx -g "daemon off;error_log /dev/stdout info;"

Require

Under the server bracket on the config file

access_log /dev/stdout;
9

Based on the official docker Nginx image this is already in place

For more information you can see the Dockerfile written for the 1-alpine version which soft link the access logs and error logs to the stdout and stderr respectively. Other docker tags also have it.

ref: https://github.com/nginxinc/docker-nginx/blob/1.23.1/stable/alpine/Dockerfile#L118-L119

7

When running Nginx in a Docker container, be aware that a volume mounted over the log dir defeats the purpose of creating a softlink between the log files and stdout/stderr in your Dockerfile, as described in @Boeboe 's answer.

In that case you can either create the softlink in your entrypoint (executed after volumes are mounted) or not use a volume at all (e.g. when logs are already collected by a central logging system).

6
# nginx.conf

worker_processes auto;

events {
    worker_connections 4096;
}

error_log  /var/log/nginx/error.log  error;
error_log  /dev/stderr  error;

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;
    access_log  /dev/stdout  main;

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

5

In docker image of PHP-FPM, i've see such approach:

# cat /usr/local/etc/php-fpm.d/docker.conf
[global]
error_log = /proc/self/fd/2

[www]
; if we send this to /proc/self/fd/1, it never appears
access.log = /proc/self/fd/2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.