我似乎无法禁用 nginx 在 docker stdout 上输出的访问日志(显示在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 和 Supervisor 禁用 nginx 访问日志?
答案1
我设法弄清楚了问题出在哪里 - 是访问日志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-fpm
docker 镜像将访问日志输出到stdout
),并且配置nginx
开始按预期工作