From 3af58f818730c27a4c18c2ee063afc830d7ff589 Mon Sep 17 00:00:00 2001 From: Bennett Goble Date: Wed, 24 Jul 2024 10:31:20 -0700 Subject: [PATCH] Add LOG_ONLY_5XX option Option that only emits access logs on 5XX series responses. --- Dockerfile | 1 + README.md | 1 + src/etc/nginx/nginx.conf.template | 10 ++++++++++ 3 files changed, 12 insertions(+) diff --git a/Dockerfile b/Dockerfile index 8388d8c..82c724a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,6 +14,7 @@ ENV LISTEN_PORT=80 ENV HEALTHCHECK_PATH="/lb-status/" ENV STATIC_LOCATIONS= ENV NO_ACCESS_LOGS=0 +ENV LOG_ONLY_5XX=0 EXPOSE 80 STOPSIGNAL SIGQUIT ENTRYPOINT ["/docker-entrypoint.sh"] diff --git a/README.md b/README.md index 886c76e..5310e15 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ Pair nginx-proxy with your favorite upstream server (wsgi, uwsgi, asgi, et al.) | `KEEPALIVE_TIMEOUT` | What value to set HTTP keepalive (This should be higher than your ELB's timeout) | Yes | 65 | | | `HEALTHCHECK_PATH` | nginx-proxy disables healthcheck path access logs, you can configure the path here | Yes | /lb-status/ | | | `NO_ACCESS_LOGS` | disable access logs completely | No | 0 | 1 | +| `LOG_ONLY_5XX` | only log 5XX HTTP status access events | No | 0 | 1 | ### Hosting Static Assets diff --git a/src/etc/nginx/nginx.conf.template b/src/etc/nginx/nginx.conf.template index 9a5c5dc..9848865 100644 --- a/src/etc/nginx/nginx.conf.template +++ b/src/etc/nginx/nginx.conf.template @@ -47,8 +47,18 @@ http { include /etc/nginx/includes/log-format.conf; + # "5XX" filter for logging + map $status $is5xx { + ~^[5] 1; + default 0; + } + + {{ if (eq .Env.LOG_ONLY_5XX "1") }} + access_log /dev/stdout json_analytics if=$is5xx; + {{ else }} # For docker logs to work, we need to output to stdout/stderr access_log /dev/stdout json_analytics; + {{ end }} include /etc/nginx/conf.d/*.conf; }