Nginx

在 docker 中使用主管禁用 nginx 訪問日誌

  • January 6, 2020

我似乎無法禁用 docker stdout 上 nginx 輸出的訪問日誌(顯示在 中docker logs <container>

這是我的 Dockerfile

FROM php:7.3.11-fpm-alpine as base

WORKDIR /var/www

# Use the default production configuration
RUN cp "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"

#Â Install dependencies
RUN set -xe \
   && apk add --no-cache bash icu-dev  \
   && apk add --no-cache nginx supervisor curl

# Configure nginx
COPY nginx.conf /etc/nginx/nginx.conf

# Configure supervisord
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf

# Make sure files/folders needed by the processes are accessable when they run under the nobody user
RUN chown -R nobody.nobody /run && \
 chown -R nobody.nobody /var/lib/nginx && \
 chown -R nobody.nobody /var/tmp/nginx && \
 chown -R nobody.nobody /var/log/nginx

# Setup document root
RUN mkdir -p /var/www/html/public

WORKDIR /var/www/html

RUN echo "<?php echo 'OK';" >> /var/www/html/public/healthcheck.php

USER nobody

# Expose the port nginx is reachable on
EXPOSE 8080

# Let supervisord start nginx & php-fpm
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]

這是 nginx.conf

worker_processes  1;
pid /run/nginx.pid;

events {
   worker_connections  1024;
}

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

   access_log off;

   proxy_buffers 16 16k;
   proxy_buffer_size 16k;

   keepalive_timeout  65;

   server {
       listen [::]:8080 default_server;
       listen 8080 default_server;
       server_name _;

       sendfile off;

       root /var/www/html/public;
       index index.php index.html;

       location = /healthcheck.php {
                   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                   include fastcgi_params;
                   fastcgi_pass 127.0.0.1:9000;
       }

   }
}

這是 supervisord.conf

[supervisord]
nodaemon=true
logfile=/dev/null
logfile_maxbytes=0
pidfile=/run/supervisord.pid

[program:php-fpm]
command=php-fpm -F
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
autorestart=false
startretries=0

[program:nginx]
command=nginx -g 'daemon off;'
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
autorestart=false
startretries=0

但仍然得到我不想要的輸出

2019-12-16 11:04:30,107 INFO supervisord started with pid 1
2019-12-16 11:04:31,110 INFO spawned: 'nginx' with pid 7
2019-12-16 11:04:31,122 INFO spawned: 'php-fpm' with pid 8
[16-Dec-2019 11:04:31] NOTICE: [pool www] 'user' directive is ignored when FPM is not running as root
[16-Dec-2019 11:04:31] NOTICE: [pool www] 'user' directive is ignored when FPM is not running as root
[16-Dec-2019 11:04:31] NOTICE: [pool www] 'group' directive is ignored when FPM is not running as root
[16-Dec-2019 11:04:31] NOTICE: [pool www] 'group' directive is ignored when FPM is not running as root
[16-Dec-2019 11:04:31] NOTICE: fpm is running, pid 8
[16-Dec-2019 11:04:31] NOTICE: ready to handle connections
2019-12-16 11:04:32,209 INFO success: nginx entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2019-12-16 11:04:32,211 INFO success: php-fpm entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
127.0.0.1 -  16/Dec/2019:11:04:34 +0000 "GET /healthcheck.php" 200
127.0.0.1 -  16/Dec/2019:11:04:34 +0000 "GET /healthcheck.php" 200
127.0.0.1 -  16/Dec/2019:11:04:34 +0000 "GET /healthcheck.php" 200

如何使用 Docker 和主管禁用 nginx 訪問日誌?

我已經設法弄清楚問題出在哪裡 - 這php-fpm是記錄這些請求的訪問日誌,而實際上根本沒有nginx記錄。要找出什麼在記錄我啟用調試日誌的內容supervisord,這給了我下面的輸出

2020-01-06 11:57:27,790 DEBG 'php-fpm' stderr output:
127.0.0.1 -  06/Jan/2020:11:57:27 +0000 "GET /healthcheck.php" 200

2020-01-06 11:57:27,790 DEBG 'php-fpm' stderr output:
127.0.0.1 -  06/Jan/2020:11:57:27 +0000 "GET /healthcheck.php" 200

2020-01-06 11:57:27,792 DEBG 'nginx' stdout output:
172.17.0.1 - - [06/Jan/2020:11:57:27 +0000] "GET /healthcheck.php HTTP/1.1" 200 12 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:72.0) Gecko/20100101 Firefox/72.0" "-"

2020-01-06 11:57:27,792 DEBG 'nginx' stdout output:
172.17.0.1 - - [06/Jan/2020:11:57:27 +0000] "GET /healthcheck.php HTTP/1.1" 200 12 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:72.0) Gecko/20100101 Firefox/72.0" "-"

然後我更新了配置(預設情況下,php-fpmdocker 映像將訪問日誌輸出到stdout),並且配置nginx開始按預期工作

引用自:https://serverfault.com/questions/995706