Skip to content

Commit 52cb1b7

Browse files
authored
feat: improve structured log format configuring it in all server definition (#115)
1 parent f264f46 commit 52cb1b7

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

docker-entrypoint.sh

+9-9
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ export PHP_PORT="${PHP_PORT:-9000}"
1212
export NGINX_PHP_READ_TIMEOUT="${NGINX_PHP_READ_TIMEOUT:-900}"
1313
export NGINX_CATCHALL_RETURN_CODE="${NGINX_CATCHALL_RETURN_CODE:-444}"
1414

15+
export NGINX_ACCESS_LOG_FORMAT="${NGINX_ACCESS_LOG_FORMAT:-main}"
16+
# If the environment is not local, we enable structured logging.
17+
if [ "${ENV:-}" != "loc" ]; then
18+
export NGINX_ACCESS_LOG_FORMAT="structured"
19+
fi
20+
1521
# If you use the rootless image the user directive is not needed
1622
if [ "$(id -u)" -ne 0 ]; then
1723
sed -i '/^user /d' /etc/nginx/nginx.conf
@@ -107,10 +113,9 @@ if [ "${NGINX_DEFAULT_SERVER_NAME}" = "_" ]; then
107113
else
108114
export DEFAULT_SERVER=""
109115
# shellcheck disable=SC2016 # The envsubst command needs to be executed without variable expansion
110-
envsubst '${PHP_HOST} ${PHP_PORT} ${NGINX_DEFAULT_SERVER_PORT} ${NGINX_DEFAULT_SERVER_NAME} ${NGINX_DEFAULT_ROOT} ${NGINX_SUBFOLDER} ${NGINX_SUBFOLDER_ESCAPED} ${NGINX_CATCHALL_RETURN_CODE} ${NGINX_HSTS_HEADER}' < /templates/catch-all-server.conf > /etc/nginx/conf.d/catch-all-server.conf
116+
envsubst '${PHP_HOST} ${PHP_PORT} ${NGINX_ACCESS_LOG_FORMAT} ${NGINX_DEFAULT_SERVER_PORT} ${NGINX_DEFAULT_SERVER_NAME} ${NGINX_DEFAULT_ROOT} ${NGINX_SUBFOLDER} ${NGINX_SUBFOLDER_ESCAPED} ${NGINX_CATCHALL_RETURN_CODE} ${NGINX_HSTS_HEADER}' < /templates/catch-all-server.conf > /etc/nginx/conf.d/catch-all-server.conf
111117
fi
112118

113-
export NGINX_ACCESS_LOG_FORMAT="${NGINX_ACCESS_LOG_FORMAT:-main}"
114119
export NGINX_DEFAULT_ROOT="${NGINX_DEFAULT_ROOT:-/var/www/html}"
115120
export NGINX_HTTPSREDIRECT="${NGINX_HTTPSREDIRECT:-0}"
116121
export NGINX_SUBFOLDER="${NGINX_SUBFOLDER:-0}"
@@ -142,11 +147,6 @@ if [ "${NGINX_OSB_RESOLVER_ENFORCE_IPV6_OFF}" = "1" ] && ! echo "${NGINX_OSB_RES
142147
export NGINX_OSB_RESOLVER="${NGINX_OSB_RESOLVER} ipv6=off"
143148
fi
144149

145-
# If the environment is not local, we enable structured logging.
146-
if [ "${ENV:-}" != "loc" ]; then
147-
export NGINX_ACCESS_LOG_FORMAT="structured"
148-
fi
149-
150150
# These lines of code have been added to provide a BC path for those
151151
# environments that still have the NGINX_OSB_BUCKET variable filled with the
152152
# old format that consist in having the value of NGINX_OSB_PUBLIC_PATH (usually
@@ -239,7 +239,7 @@ envsubst '${PHP_HOST} ${PHP_PORT} ${NGINX_ACCESS_LOG_FORMAT} ${NGINX_DEFAULT_SER
239239

240240
if [ "${NGINX_SUBFOLDER}" != 0 ]; then
241241
# shellcheck disable=SC2016 # The envsubst command needs to be executed without variable expansion
242-
envsubst '${PHP_HOST} ${PHP_PORT} ${NGINX_DEFAULT_SERVER_PORT} ${NGINX_DEFAULT_SERVER_NAME} ${NGINX_DEFAULT_ROOT} ${NGINX_SUBFOLDER} ${NGINX_SUBFOLDER_ESCAPED} ${NGINX_XFRAME_OPTION_VALUE} ${NGINX_HSTS_HEADER}' < /templates/subfolder.conf > /etc/nginx/conf.d/default.conf
242+
envsubst '${PHP_HOST} ${PHP_PORT} ${NGINX_ACCESS_LOG_FORMAT} ${NGINX_DEFAULT_SERVER_PORT} ${NGINX_DEFAULT_SERVER_NAME} ${NGINX_DEFAULT_ROOT} ${NGINX_SUBFOLDER} ${NGINX_SUBFOLDER_ESCAPED} ${NGINX_XFRAME_OPTION_VALUE} ${NGINX_HSTS_HEADER}' < /templates/subfolder.conf > /etc/nginx/conf.d/default.conf
243243
fi
244244

245245
# Handle robots.txt and sitemap directive
@@ -344,7 +344,7 @@ if [ "${NGINX_REDIRECT_FROM_TO_WWW}" -eq 1 ] && [ "${NGINX_DEFAULT_SERVER_NAME}"
344344
print "/etc/nginx/conf.d/from-to-www.conf - Creating a redirect from ${DOMAIN_FROM} to ${DOMAIN_TO}"
345345
# shellcheck disable=SC2016 # The envsubst command needs to be executed without variable expansion
346346
DOMAIN_FROM=${DOMAIN_FROM} DOMAIN_TO=${DOMAIN_TO} \
347-
envsubst '${DOMAIN_FROM} ${DOMAIN_TO} ${NGINX_DEFAULT_SERVER_PORT} ${DEFAULT_SERVER} ${NGINX_HSTS_HEADER}' < /templates/from-to-www.conf.tpl \
347+
envsubst '${DOMAIN_FROM} ${DOMAIN_TO} ${NGINX_DEFAULT_SERVER_PORT} ${DEFAULT_SERVER} ${NGINX_ACCESS_LOG_FORMAT} ${NGINX_HSTS_HEADER}' < /templates/from-to-www.conf.tpl \
348348
| tee -a /etc/nginx/conf.d/from-to-www.conf >/dev/null
349349
else
350350
print "/etc/nginx/conf.d/from-to-www.conf - Skipping redirect from ${DOMAIN_FROM} to ${DOMAIN_TO} because it already exists"

templates/catch-all-server.conf

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ map $scheme://$host$request_uri $new_domain_uri {
77
}
88

99
server {
10+
access_log /var/log/nginx/access.log ${NGINX_ACCESS_LOG_FORMAT};
11+
1012
listen ${NGINX_DEFAULT_SERVER_PORT} default_server;
1113

1214
# The _ it is just one of a myriad of invalid domain

templates/from-to-www.conf.tpl

+4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
server {
2+
access_log /var/log/nginx/access.log ${NGINX_ACCESS_LOG_FORMAT};
3+
24
server_name ${DOMAIN_FROM};
35
listen ${NGINX_DEFAULT_SERVER_PORT} ${DEFAULT_SERVER};
6+
47
#hstsheader
58
#httpsredirect
9+
610
set $request_proto $scheme;
711
if ($http_x_forwarded_proto = "https") {
812
set $request_proto "https";

templates/subfolder.conf

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ map $http_x_forwarded_proto $fastcgi_https {
99
}
1010

1111
server {
12+
access_log /var/log/nginx/access.log ${NGINX_ACCESS_LOG_FORMAT};
13+
1214
server_name ${NGINX_DEFAULT_SERVER_NAME};
1315
listen ${NGINX_DEFAULT_SERVER_PORT};
1416

@@ -22,7 +24,6 @@ server {
2224

2325
include /etc/nginx/conf.d/fragments/*.conf;
2426

25-
2627
# Just return for google gclb healthcheck.
2728
if ($http_user_agent = 'GoogleHC/1.0') {
2829
return 200;

0 commit comments

Comments
 (0)