注意:日志记录已禁用,但通知仍然出现!
我想要一个干净的日志,只显示我的 Docker 容器的错误,但 nginx 只是用噪音淹没命令行,即使我禁用一切,是否有任何优雅的方法(例如不使用 grep 作为入口点)来禁用这些通知?
我已经在使用NGINX_ENTRYPOINT_QUIET_LOGS=1
环境变量、-q
参数和完全禁用错误日志(这不是最终目标,仅用于测试)。
$ docker run -e "NGINX_ENTRYPOINT_QUIET_LOGS=1" nginx:alpine "nginx" "-q" "-g" "daemon off; error_log /dev/null emerg;"
2024/02/15 09:38:02 [notice] 1#1: using the "epoll" event method
2024/02/15 09:38:02 [notice] 1#1: nginx/1.25.4
2024/02/15 09:38:02 [notice] 1#1: built by gcc 12.2.1 20220924 (Alpine 12.2.1_git20220924-r10)
2024/02/15 09:38:02 [notice] 1#1: OS: Linux 6.7.4-arch1-1
2024/02/15 09:38:02 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1073741816:1073741816
2024/02/15 09:38:02 [notice] 1#1: start worker processes
2024/02/15 09:38:02 [notice] 1#1: start worker process 30
2024/02/15 09:38:02 [notice] 1#1: start worker process 31
2024/02/15 09:38:02 [notice] 1#1: start worker process 32
2024/02/15 09:38:02 [notice] 1#1: start worker process 33
2024/02/15 09:38:02 [notice] 1#1: start worker process 34
2024/02/15 09:38:02 [notice] 1#1: start worker process 35
2024/02/15 09:38:02 [notice] 1#1: start worker process 36
2024/02/15 09:38:02 [notice] 1#1: start worker process 37
2024/02/15 09:38:02 [notice] 1#1: start worker process 38
2024/02/15 09:38:02 [notice] 1#1: start worker process 39
2024/02/15 09:38:02 [notice] 1#1: start worker process 40
2024/02/15 09:38:02 [notice] 1#1: start worker process 41
2024/02/15 09:38:02 [notice] 1#1: start worker process 42
2024/02/15 09:38:02 [notice] 1#1: start worker process 43
2024/02/15 09:38:02 [notice] 1#1: start worker process 44
2024/02/15 09:38:02 [notice] 1#1: start worker process 45
2024/02/15 09:38:02 [notice] 1#1: start worker process 46
2024/02/15 09:38:02 [notice] 1#1: start worker process 47
2024/02/15 09:38:02 [notice] 1#1: start worker process 48
2024/02/15 09:38:02 [notice] 1#1: start worker process 49
2024/02/15 09:38:02 [notice] 1#1: start worker process 50
2024/02/15 09:38:02 [notice] 1#1: start worker process 51
2024/02/15 09:38:02 [notice] 1#1: start worker process 52
2024/02/15 09:38:02 [notice] 1#1: start worker process 53
^C2024/02/15 09:38:04 [notice] 1#1: signal 2 (SIGINT) received, exiting
2024/02/15 09:38:04 [notice] 30#30: exiting
2024/02/15 09:38:04 [notice] 32#32: exiting
2024/02/15 09:38:04 [notice] 33#33: exiting
2024/02/15 09:38:04 [notice] 39#39: exiting
2024/02/15 09:38:04 [notice] 37#37: exiting
2024/02/15 09:38:04 [notice] 40#40: exiting
2024/02/15 09:38:04 [notice] 38#38: exiting
2024/02/15 09:38:04 [notice] 31#31: exiting
2024/02/15 09:38:04 [notice] 42#42: exiting
2024/02/15 09:38:04 [notice] 43#43: exiting
2024/02/15 09:38:04 [notice] 35#35: exiting
2024/02/15 09:38:04 [notice] 34#34: exiting
2024/02/15 09:38:04 [notice] 46#46: exiting
2024/02/15 09:38:04 [notice] 36#36: exiting
2024/02/15 09:38:04 [notice] 45#45: exiting
2024/02/15 09:38:04 [notice] 47#47: exiting
2024/02/15 09:38:04 [notice] 49#49: exiting
2024/02/15 09:38:04 [notice] 48#48: exiting
2024/02/15 09:38:04 [notice] 41#41: exiting
2024/02/15 09:38:04 [notice] 44#44: exiting
2024/02/15 09:38:04 [notice] 51#51: exiting
2024/02/15 09:38:04 [notice] 50#50: exiting
2024/02/15 09:38:04 [notice] 52#52: exiting
2024/02/15 09:38:04 [notice] 53#53: exiting
2024/02/15 09:38:04 [notice] 36#36: exit
2024/02/15 09:38:04 [notice] 33#33: exit
2024/02/15 09:38:04 [notice] 30#30: exit
2024/02/15 09:38:04 [notice] 49#49: exit
2024/02/15 09:38:04 [notice] 41#41: exit
2024/02/15 09:38:04 [notice] 37#37: exit
2024/02/15 09:38:04 [notice] 42#42: exit
2024/02/15 09:38:04 [notice] 43#43: exit
2024/02/15 09:38:04 [notice] 35#35: exit
2024/02/15 09:38:04 [notice] 52#52: exit
2024/02/15 09:38:04 [notice] 31#31: exit
2024/02/15 09:38:04 [notice] 34#34: exit
2024/02/15 09:38:04 [notice] 45#45: exit
2024/02/15 09:38:04 [notice] 44#44: exit
2024/02/15 09:38:04 [notice] 46#46: exit
2024/02/15 09:38:04 [notice] 39#39: exit
2024/02/15 09:38:04 [notice] 50#50: exit
2024/02/15 09:38:04 [notice] 32#32: exit
2024/02/15 09:38:04 [notice] 53#53: exit
2024/02/15 09:38:04 [notice] 40#40: exit
2024/02/15 09:38:04 [notice] 48#48: exit
2024/02/15 09:38:04 [notice] 51#51: exit
2024/02/15 09:38:04 [notice] 47#47: exit
2024/02/15 09:38:04 [notice] 38#38: exit
2024/02/15 09:38:04 [notice] 1#1: signal 14 (SIGALRM) received
2024/02/15 09:38:04 [notice] 1#1: signal 17 (SIGCHLD) received from 40
2024/02/15 09:38:04 [notice] 1#1: worker process 40 exited with code 0
2024/02/15 09:38:04 [notice] 1#1: worker process 42 exited with code 0
2024/02/15 09:38:04 [notice] 1#1: signal 29 (SIGIO) received
2024/02/15 09:38:04 [notice] 1#1: signal 17 (SIGCHLD) received from 42
2024/02/15 09:38:04 [notice] 1#1: worker process 39 exited with code 0
2024/02/15 09:38:04 [notice] 1#1: signal 29 (SIGIO) received
2024/02/15 09:38:04 [notice] 1#1: signal 17 (SIGCHLD) received from 37
2024/02/15 09:38:04 [notice] 1#1: worker process 37 exited with code 0
2024/02/15 09:38:04 [notice] 1#1: worker process 46 exited with code 0
2024/02/15 09:38:04 [notice] 1#1: signal 29 (SIGIO) received
2024/02/15 09:38:04 [notice] 1#1: signal 17 (SIGCHLD) received from 46
2024/02/15 09:38:04 [notice] 1#1: worker process 32 exited with code 0
2024/02/15 09:38:04 [notice] 1#1: worker process 48 exited with code 0
2024/02/15 09:38:04 [notice] 1#1: signal 29 (SIGIO) received
2024/02/15 09:38:04 [notice] 1#1: signal 17 (SIGCHLD) received from 47
2024/02/15 09:38:04 [notice] 1#1: worker process 47 exited with code 0
2024/02/15 09:38:04 [notice] 1#1: signal 29 (SIGIO) received
2024/02/15 09:38:04 [notice] 1#1: signal 17 (SIGCHLD) received from 44
2024/02/15 09:38:04 [notice] 1#1: worker process 44 exited with code 0
2024/02/15 09:38:04 [notice] 1#1: signal 29 (SIGIO) received
2024/02/15 09:38:04 [notice] 1#1: signal 17 (SIGCHLD) received from 50
2024/02/15 09:38:04 [notice] 1#1: worker process 45 exited with code 0
2024/02/15 09:38:04 [notice] 1#1: worker process 50 exited with code 0
2024/02/15 09:38:04 [notice] 1#1: signal 29 (SIGIO) received
2024/02/15 09:38:04 [notice] 1#1: signal 17 (SIGCHLD) received from 45
2024/02/15 09:38:04 [notice] 1#1: worker process 51 exited with code 0
2024/02/15 09:38:04 [notice] 1#1: signal 29 (SIGIO) received
2024/02/15 09:38:04 [notice] 1#1: signal 17 (SIGCHLD) received from 33
2024/02/15 09:38:04 [notice] 1#1: worker process 33 exited with code 0
2024/02/15 09:38:04 [notice] 1#1: worker process 43 exited with code 0
2024/02/15 09:38:04 [notice] 1#1: worker process 34 exited with code 0
2024/02/15 09:38:04 [notice] 1#1: signal 29 (SIGIO) received
2024/02/15 09:38:04 [notice] 1#1: signal 17 (SIGCHLD) received from 34
2024/02/15 09:38:04 [notice] 1#1: signal 17 (SIGCHLD) received from 52
2024/02/15 09:38:04 [notice] 1#1: worker process 52 exited with code 0
2024/02/15 09:38:04 [notice] 1#1: signal 29 (SIGIO) received
2024/02/15 09:38:04 [notice] 1#1: signal 17 (SIGCHLD) received from 31
2024/02/15 09:38:04 [notice] 1#1: worker process 31 exited with code 0
2024/02/15 09:38:04 [notice] 1#1: signal 29 (SIGIO) received
2024/02/15 09:38:04 [notice] 1#1: signal 17 (SIGCHLD) received from 36
2024/02/15 09:38:04 [notice] 1#1: worker process 36 exited with code 0
2024/02/15 09:38:04 [notice] 1#1: worker process 41 exited with code 0
2024/02/15 09:38:04 [notice] 1#1: worker process 53 exited with code 0
2024/02/15 09:38:04 [notice] 1#1: signal 29 (SIGIO) received
2024/02/15 09:38:04 [notice] 1#1: signal 17 (SIGCHLD) received from 49
2024/02/15 09:38:04 [notice] 1#1: worker process 30 exited with code 0
2024/02/15 09:38:04 [notice] 1#1: worker process 38 exited with code 0
2024/02/15 09:38:04 [notice] 1#1: worker process 49 exited with code 0
2024/02/15 09:38:04 [notice] 1#1: signal 29 (SIGIO) received
2024/02/15 09:38:04 [notice] 1#1: signal 17 (SIGCHLD) received from 30
2024/02/15 09:38:04 [notice] 1#1: signal 17 (SIGCHLD) received from 35
2024/02/15 09:38:04 [notice] 1#1: worker process 35 exited with code 0
2024/02/15 09:38:04 [notice] 1#1: exit
PS:问题从nginx版本1.20.0开始。版本1.19.10(最新的1.19版本)没有这些通知,但出于安全漏洞的考虑,我不想使用这样的旧版本。
然而,变更日志中并未提及此更改http://nginx.org/en/CHANGES-1.20:
Changes with nginx 1.20.0 20 Apr 2021
*) 1.20.x stable branch.
PPS:使用此测试禁用所有日志记录error_log /dev/null emerg;
,这些消息似乎是由 nginx 在正常日志记录之外发出的。
答案1
您使用的 Docker 映像nginx:alpine
使用包含以下行的 Nginx 配置:
error_log /var/log/nginx/error.log notice;
您应该提取提供的配置文件,对其进行修改,然后启动容器并将修改后的配置文件安装在正确的位置。
只需
cat /etc/nginx/nginx.conf
在容器中运行并将输出重定向到文件(这里我my-nginx.conf
在当前目录中使用)即可获取提供的配置文件:docker run --rm --entrypoint=cat nginx:alpine /etc/nginx/nginx.conf >my-nginx.conf
使用您最喜欢的编辑器修改文件,将上面指示的行上的单词更改
notice
为。error
这将使服务日志错误消息或消息具有更高的优先级。使用 GNUsed
,你可以这样做:sed -i '/^error_log.*notice;/ s/notice/error/' my-nginx.conf
启动容器,并在 处挂载(只读)修改后的配置文件
/etc/nginx/nginx.conf
。在这里,我只是测试run --rm
:docker run --rm -v "$PWD/my-nginx.conf":/etc/nginx/nginx.conf:ro nginx:alpine
当我在本地执行此操作时,我得到以下输出,表明日志级别确实不再是notice
:
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
另请参阅:Nginx Docker Hub 页面的“自定义配置”部分:https://hub.docker.com/_/nginx
答案2
error_log warn;
在生产中使用。及以上notice
级别才适合开发。