Have nginx access_log and error_log log to STDOUT and STDERR of master process

后端 未结 6 2033
一整个雨季
一整个雨季 2020-12-12 10:18

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:

相关标签:
6条回答
  • 2020-12-12 10:52

    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).

    0 讨论(0)
  • 2020-12-12 10:54

    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
    
    0 讨论(0)
  • 2020-12-12 11:08

    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

    0 讨论(0)
  • 2020-12-12 11:08

    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

    0 讨论(0)
  • 2020-12-12 11:08
    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 讨论(0)
  • 2020-12-12 11:11

    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;
    
    0 讨论(0)
提交回复
热议问题